Skip to content Skip to sidebar Skip to footer

Copy A File Line By Line In Python

I am writing a python program to copy a file line by line into a new file. The code I have is below in which I am using a loop to copy the file line by line. However since the num

Solution 1:

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

for line in f:
    copy.write(line)

From the docs on file objects:

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

>>> for line in f:
        print line,

Solution 2:

Files can be iterated directly, without the need for an explicit call to readline:

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

Solution 3:

See shutil module for better ways of doing this than copying line-by-line:

shutil.copyfile(src, dst)

Copy the contents (no metadata) of the file named src to a file named dst. dst must be the complete target file name; look at shutil.copy() for a copy that accepts a target directory path. If src and dst are the same files, Error is raised. The destination location must be writable; otherwise, an 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 given as strings.

Edit: Your question says you are copying line-by-line because the source file is volatile. Something smells wrong about your design. Could you share more details regarding the problem you are solving?

Solution 4:

Writing line by line can be slow when working with large data. You can accelerate the read/write operations by reading/writing a bunch of lines all at once. Please refer to my answer to a similar question here

Post a Comment for "Copy A File Line By Line In Python"