Writing to the same file after reading from the file

My goal is to read a line from a file, remove the spaces at the end, and write back to the same file. I tried the following code:

with open(filename, 'r+') as f: for i in f: f.write(i.rstrip()+"\n") 

It seems to be written at the end of the file, keeping the original data in the file intact. I know that using f.seek(0) will cause the pointer to go back to the beginning of the file, which I assume will be somehow necessary for this solution.

Could you please advise if there is another approach for this or on the right patch I just need to add more logic to the code?

+6
source share
2 answers

Use a temporary file. Python provides tools for creating temporary files in safe mode. Example call below: python modify.py target_filename

  import tempfile import sys def modify_file(filename): #Create temporary file read/write t = tempfile.NamedTemporaryFile(mode="r+") #Open input file read-only i = open(filename, 'r') #Copy input file to temporary file, modifying as we go for line in i: t.write(line.rstrip()+"\n") i.close() #Close input file t.seek(0) #Rewind temporary file to beginning o = open(filename, "w") #Reopen input file writable #Overwriting original file with temporary file contents for line in t: o.write(line) t.close() #Close temporary file, will cause it to be deleted if __name__ == "__main__": modify_file(sys.argv[1]) 

Links here: http://docs.python.org/2/library/tempfile.html

+5
source

The problem with your approach is that you need both an input stream and an output stream that can point to different places in one file. If you want to use f.seek() , you will need to save the position with f.tell() after each read and write. For instance:

 f = open(filename, 'r+') while True: i = f.readline() if i == '': break in = f.tell() f.seek(out) f.write(i.rstrip()+"\n") out = f.tell() f.seek(in) 

But this is confusing and error prone. If the file is not too large, why not read it all in memory and then write it back?

 in = open(filename, 'r') lines = in.read() in.close() out = open(filename, 'w') out.write([line.rstrip()+'\n' for line in lines.split('\n')]) out.close() 

If the file is too large to fit in memory, write the lines to a temporary file and then rename the file when you are done:

 out = open(filename+'.tmp', 'w') with open(filename, 'r') as f: for i in f: out.write(i.rstrip()+"\n") out.close() os.rename(filename+'.tmp', filename) 
+1
source

Source: https://habr.com/ru/post/949433/


All Articles