best way to copy a file [Q]

Tim Peters tim_one at email.msn.com
Sun Apr 11 15:45:07 EDT 1999


[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






More information about the Python-list mailing list