best way to copy a file [ANSWER]

Bruno Mattarollo brunomadv at ciudad.com.ar
Sun Apr 11 15:49:50 EDT 1999


Thanks Hans. It worked... I didn't used the 'rb' and 'wb' when opening the
files...

I should stop working on sundays ... :-) Well, I should stop working
everyday ;)

/B

Bruno Mattarollo <bruno at gaiasur.com.ar>
... proud to be a PSA member <http://www.python.org/psa>

> -----Original Message-----
> From: Hans Nowak [mailto:ivnowa at hvision.nl]
> Sent: Sunday, April 11, 1999 5:37 PM
> To: Bruno Mattarollo; python-list at cwi.nl
> Subject: Re: best way to copy a file [Q]
>
>
> 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