Mailing List Archive

best way to copy a file [Q]
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+ ...

TIA and excuse my english ... :-)

/B

Bruno Mattarollo <bruno@gaiasur.com.ar>
... proud to be a PSA member <http://www.python.org/psa>
best way to copy a file [Q] [ In reply to ]
In article <000201be8441$7965d4d0$6eba0ac8@kuarajy.infosys.com.ar>, "Bruno
Mattarollo" <brunomadv@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
best way to copy a file [Q] [ In reply to ]
[Bruno Mattarollo]
> 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 file ... How can I do a 'copy'
> ...? I need to run this on NT ...:(

If it's to run only under Windows systems, will go fastest to build up an
xcopy command line and pass it to os.system.

> And I don't want to open a shell to do a copy from there... I also tryied
> fd.read() ... No success neither.

Oh sure -- that works fine. The raw "no success" conveys no information,
though, so not enough clues to guess what part you didn't get right. The
most common cause for screwing this up is forgetting to open Windows files
in binary mode.

To see how it's done, look at the source code for shutil.py in your Python's
Lib directory. shutil.copyfile is what you're looking for, if you need a
cross-platform function.

all-obvious-to-everyone-who-already-knows-it<wink>-ly y'rs - tim
best way to copy a file [Q] [ In reply to ]
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!