Copy a file line by line in python

前端 未结 4 1609
半阙折子戏
半阙折子戏 2021-01-12 17:48

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.

Howeve

4条回答
  •  灰色年华
    2021-01-12 18:41

    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?

提交回复
热议问题