best way to copy a file [Q]

Hans Nowak ivnowa at hvision.nl
Sun Apr 11 16:37:07 EDT 1999


On 11 Apr 99, Ce'Nedra took her magical amulet and heard Bruno Mattarollo say:

>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. 

Sure can:

fin = open("myfile", "rb")   # notice the 'rb'
fout = open("target", "wb")  # ...and the 'wb'
data = fin.read(1000)        # or any amount of bytes you want to read
while data:
    fout.write(data)
    data = fin.read(1000)
fin.close()
fout.close()

This should work. For large files, a larger number than 1000 may be 
desirable.

+  Hans Nowak  (Zephyr Falcon)
+  Homepage (under construction): http://www.cuci.nl/~hnowak/
+  You call me a masterless man. You are wrong. I am my own master.
+  May a plumber throw eggs at your dead presidents!




More information about the Python-list mailing list