This tag refers to the process of writing text or data to a file.
Questions tagged [file-writing]
932 questions
-3
votes
1 answer
Python: FileNotFoundError: [Errno 2] No such file or directory error
Want to name a file with open function, but error occur.
Can not figure out what is the problem, with just img_name = 'image1' everything is OK.
Thanks
img_name = f'{id_}/image-{img_num}.jpg'
with open(img_name, 'wb') as img_file:

Arthur
- 109
- 8
-3
votes
3 answers
Why can't I read a line from a file and print it?
I have the following code:
test_file = open("test.txt","r")
line1 = test_file.readline(1)
test_file.close()
line1 = int(line1)
print(line1)
I have 12 written into the file.
I simply get the output 1.
Can someone please explain what is wrong with…

francisco
- 37
- 4
-3
votes
1 answer
Python: improve performance in log writing to file
I am writing a piece of code that involves generation of new parameter values over a double FOR loop and store these values to a file. The loop iteration count can go as high as 10,000 * 100,000. I have stored the variable values in a string, which…

akzhere
- 323
- 4
- 11
-3
votes
4 answers
Can't write to a .txt file in Python
This is my code:
import os
os.chdir("C:\\Users\\satvi_000\\Downloads")
if os.path.exists('new_file.txt')==False:
create_file= open("new_file.txt",'w') #This is just to create the file
#in…

Satvik Gupta
- 53
- 1
- 2
- 7
-3
votes
2 answers
Write strings in a file with quotes between quotes
what I am doing is writing a .js file with c++ and it will be used in a .html file to draw an organizational chart.
I am actually making an abstract syntax tree. for this, I write the tree nodes in a js file like the following
var nodes = [
"int…

Tarek Salah uddin Mahmud
- 924
- 8
- 18
-3
votes
1 answer
Formatting string to get just words in a column
I have a text:
c:\MyMP3s\4 Non Blondes\Bigger!\Faster, More!_Train.mp3
I want to remove form this text these characters: :,\!._
And format the text then like this:
c
MyMP3s
4
Non
Blindes
Bigger
Faster
More
Train
mp3
And write all of this in a…

N.Der
- 31
- 1
- 8
-3
votes
1 answer
How to write to a .txt file
I need help trying to convert an ASCII list to a string list, I have already tried this:
ciphertext = str[Nintendo65]
However it keeps outputting this: TypeError: 'type' object is not subscriptable

Zac Cotterell
- 105
- 1
- 1
- 8
-3
votes
2 answers
File Writing Python, How to print variable to txt file
I am trying to print a variable of a score and name to a .txt file using python.
import random
import csv
import operator
import datetime
now = datetime.datetime.now() ## gets the exact time of when the user begins the test.
def main():
…

Rasnaamt
- 21
- 1
- 1
- 4
-4
votes
1 answer
Is there a way to avoid writing duplicate lines to a file in Java?
I have a simple question:
I have some code that writes lines of data to a text file in Java. One of the method calls that writes data to the file is located within a method that is called more frequently than the data itself changes.
As a result,…

Eric Bush
- 147
- 1
- 1
- 10
-4
votes
1 answer
program creates a file, but won't write to it
I'm very novice at C but I'm trying to learn. I'm trying right now to make a program read from a csv file and take only it's integers from it, and write those out to a seperate .txt file. I'm not seeing why it will create a file, but why it won't…

Ian McMahon
- 3
- 3
-4
votes
2 answers
Error when writing file
I am trying to write numbers into a txt file.
But i get the error message telling me that the values have to be in string format not in int.
a = [1,2,3,4,5]
b = [2,6,4,3,2]
with open('writefile.txt','w') as f:
for i in range(len(a)):
…
-4
votes
3 answers
Insert new data for each text line in python
I have a text file that looks like this:
1,004,59
1,004,65
1,004,69
1,005,55
1,005,57
1,006,53
1,006,59
1,007,65
1,007,69
1,007,55
1,007,57
1,008,53
Want to create new text file that will be inserted by 'input', something like…

Shubham Kuse
- 135
- 1
- 2
- 10
-4
votes
1 answer
Writing to a file in C not working
I have the following code, and I keep on getting the "Program has stopped working" error after I have input the characters.
I have done some debugging and found that the issue is in the writing to the file part, however I cannot find the issue.
Can…

quantrillliam
- 33
- 1
- 4
-5
votes
2 answers
Writing integers into a new file
So I am trying to open a new file, and write in to that file all the values of n.
for n in [4, 7, 8, 10, 6, 3, 5, 13]:
if n > 5:
print(n)
b = open('new', 'w')
b.write(n)
It writes the numbers in as a string and only writes in 13, the…

Ben
- 25
- 2
-6
votes
2 answers
Why isn't my file writing method working?
import os
books = open(os.path.expanduser("~/Desktop/books.txt")).read()
b= books.split('\n')
del b[-1]
book={}
for i in b:
b1=i.split('\t')
book[b1[0]]=[b1[1],b1[2],b1[3]]
def all_book():
print "The Book List"
books =…