best way to copy a file [Q]
Matthew Dixon Cowles
matt at mondoinfo.com
Sun Apr 11 14:59:56 EDT 1999
In article <000201be8441$7965d4d0$6eba0ac8 at kuarajy.infosys.com.ar>, "Bruno
Mattarollo" <brunomadv at ciudad.com.ar> wrote:
> Hi!
>
> I need to copy a file (can be binary or ascii) from one path to
> another. I
> have tryied to do:
> line = fd.readline()
> while line:
> fd2.write(line)
> line = fd.readline()
> fd.close()
> fd2.close()
>
> It only works for ascii files ... How can I do a 'copy' ...? I
need to
> run
> this on NT ...:( And I don't want to open a shell to do a copy from
> there... I also tryied fd.read() ... No success neither. I have looked
> unsuccesfully throughout the documentation and didn't find a 'filecopy'
> method. The files can range from 1KB to 600MB+ ...
Bruno,
You need to open the files in "binary" mode since Windows makes a
distinction between text and binary files. I understand that something
like
in=open("file","rb")
out=open("file2","wb")
should work. Also, you might want to reconsider using readline() for
binary files. If a large binary file happened not to contain \r\n,
readline() might try to grab an unreasonably large piece of the file:
>>> foo=open("/dev/zero")
>>> foo.readline()
Bus error
The function copyfile() in the standard module shutil would probably work
well for you. But now you know why <wink>.
Regards,
Matt
More information about the Python-list
mailing list