wasm-demo/demo/ermis-f/python_m/cur/0151

57 lines
1.7 KiB
Plaintext
Raw Permalink Normal View History

From: matt at mondoinfo.com (Matthew Dixon Cowles)
Date: Sun, 11 Apr 1999 13:59:56 -0500
Subject: best way to copy a file [Q]
References: <000201be8441$7965d4d0$6eba0ac8@kuarajy.infosys.com.ar>
Message-ID: <matt-1104991359560001@scotch.mondoinfo.com>
Content-Length: 1370
X-UID: 151
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