This question already has answers here:
How to append a new row to an old CSV file in Python?
(8 answers)
Closed 4 years ago.
How does one edit specific lines of a file?
Example
What's your name?
What do you do?
What's your favourite colour?
And then after running a specific program, the outcome will be..
What's your name? : Thatile
What do you do? : I am a student
What's your favourite colour? : Black
Using file.seek() overwrites and I want to keep the original text in the file, just edit lines
A simple solution (though not memory efficient) is to read the whole file to memory (using file.read() or file.readline() in a loop), edit the data you obtained to append the answers, then write the modified data to the original file (overwriting its original content).
Again, this is not memory efficient and could take a long time on a large file.
Short answer: you cannot "edit a specific line in a file", you have to read the whole file and overwrite it with the edited content. The safe and memory-efficient way is to
open a temporary file for writing
open the original file for reading
loop over the original file's content and for each line, update the line with your new content and write it to the temporary file
close all files and replace the original with the temporary one
Related
This question already has answers here:
read the whole file at once
(2 answers)
Closed 7 months ago.
Let us say I wanted to read a whole file at once instead of going through it line by line (let's say for example to speed up retrieval of the times 'i' occurs in the file). How would I go about reading it as a whole instead of the lines in which it is written?
with open("file.ext", 'r') as f:
data = f.read()
As others have mentioned, you can find an official Python tutorial for Reading and Writing Files which explains this. You can also see the Methods of File Objects section, which explains the use of f.read(size) and f.readline() and the difference between between them.
This question already has answers here:
How to delete a specific line in a file?
(17 answers)
Closed 3 years ago.
So my problem is: I am working with Python on a Discord bot (using discord.py).
However, I want the bot to read a txt file, choose one line out of it and delete this line after that.
This is how far I've come:
list = open(r"C:\Users\Max\Documents\Python\Discord Bots\Test File\Text\text.txt","r+")
readlist = list.readlines()
text = random.choice(readlist)
for readlist in list: <-- I guess there is the problem
readlist = "\n" <-- here too
list.close()
You need to write the lines back to the file. Otherwise, you're just modifying the local variable. To do this with the same file object, you'll need to seek to the beginning of the file before you write. You'll probably also want to use a list of lines, e.g. from readlines, rather than iterating through the lines while you're writing them. You can then truncate any additional lines you didn't write over at the end.
For more information about reading and writing files and I/O, see https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files and https://docs.python.org/3/library/io.html.
Also, in your code, you're shadowing readlist and the built-in list.
This also has nothing to do with discord.py or Discord bots.
This question already has answers here:
Deleting a line in a file
(4 answers)
Closed 4 years ago.
I want to read one text file line by line and delete the line. So steps are like below.
Read the line
Delete the line
Repeat step 1 and 2 until the file is not empty
For example data in file is like below.
1,2,3,4,5
a,b,c,d,e
q,w,e,r,t
a,s,d,f,g
So, steps will be ...
while Reading
Read 1,2,3,4,5
Delete 1,2,3,4,5
then next line
Read a,b,c,d,e
Delete a,b,c,d,e
..
..
and so on until the file is not empty.
I know Python well, but I am not able to do it.
Don't even try!
At its lowest level, a file is a sequential stream of bytes. You can read bytes from that stream, overwrite bytes, and position your cursor (position where to read of write) anywhere between the beginning and the end of a file. When you are at end of file and write, you extend the file. You can also truncate a file (remove everything past the cursor).
So there is no way to remove the initial part of a file! It is commonly done by writing a new file containing what needs to be kept remove the original file and rename the new file to give it the original name.
I assume that you are trying the wrong tool to solve your real problem...
This question already has answers here:
How do I append to a file?
(13 answers)
Closed 8 years ago.
I need to add my program's outputs to a saved file every time I run the program without overwriting the previous outputs.
This is how I'm writing to a file now:
lead.write(str(alldates))
Any ideas?
Here, you should open the file in append mode:
append_text = str(alldates)
with open('my_file.txt', 'a') as lead:
lead.write(append_text)
There are lots of specialised ways to open files for different purposes, and you can read about them in the documentation.
You should open the file in append mode.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Fastest Way to Delete a Line from Large File in Python
How to edit a line in middle of txt file without overwriting everything?
I know I can read every line into a list, remove a line, then write the list back.
But the file is large, is there a way to remove a part in the middle of the file, and needn't rewrite the whole file?
I don't know if a way to change the file in place, even using low-level file system commands, but you don't need to load it into a list, so you can do this without a large memory footprint:
with open('input_file', 'r') as input_file:
with open('output_file', 'w') as output_file:
for line in input_file:
if should_delete(line):
pass
else:
output_file.write(line)
This assumes that the section you want to delete is a line in a text file, and that should_delete is a function which determines whether the line should be kept or deleted. It is easy to change this slightly to work with a binary file instead, or to use a counter instead of a function.
Edit: If you're dealing with a binary file, you know the exact position you want to remove, and its not too near the start of the file, you may be able to optimise it slightly with io.IOBase.truncate (see http://docs.python.org/2/library/io.html#io.IOBase). However, I would only suggest pursuing this if the a profiler indicates that you really need to optimise to this extent.