Copy file line by line in python

I am writing a python program to copy a file line by line to a new file. Below is the code in which I use a loop to copy a file line by line.

However, since the number of lines in a file can change, is there a way to copy a file line by line in python without using a loop that relies on numbers and instead relies on something like an EOF character to break the loop?

import os import sys i = 0 f = open("C:\\Users\\jgr208\\Desktop\\research_12\\sap\\beam_springs.$2k","r") copy = open("C:\\Users\\jgr208\\Desktop\\research_12\\sap\\copy.$2k","wt") #loop that copies file line by line and terminates loop when i reaches 10 while i < 10: line = f.readline() copy.write(str(line)) i = i +1 f.close() copy.close() 
+4
source share
4 answers

You can iterate through the lines in a file object in Python by iterating over the file object itself:

 for line in f: copy.write(line) 

From docs in file objects :

An alternative approach to reading lines is to loop over a file object. This memory is efficient, fast, and leads to simpler code:

 >>> for line in f: print line, 
+11
source

Files can be iterated directly, without the need to explicitly call readline :

 f = open("...", "r") copy = open("...", "w") for line in f: copy.write(line) f.close() copy.close() 
+7
source

See the shutil module for a better way to do this than copying one at a time:

shutil.copyfile(src, dst)

Copy the contents (without metadata) of a file named src to a file named dst. dst should be the full target file name; look at shutil.copy () for a copy that takes the target directory path. If src and dst are the same files, Error increases. Destination must be writable; otherwise, a IOError exception will be raised. If dst already exists, it will be replaced. Special files, such as character or block devices and pipes, cannot be copied with this function. src and dst are path names specified as strings.

Edit: Your question says that you are copying line by line because the source file is unstable. Something doesn't reflect so badly on your design. Could you share more detailed information about the problem you are solving?

+3
source

Writing line by line may slow down when working with big data. You can speed up read / write operations while browsing / writing a bunch of lines. Please refer to my answer to a similar question here

0
source

All Articles