Path: csiph.com!news.fcku.it!bofh.it!diesel.cu.mi.it!.POSTED!not-for-mail From: pecore@pascolo.org Newsgroups: it.comp.lang.python Subject: Re: Copiare file "a pezzi" Date: Tue, 19 Apr 2016 01:15:12 +0200 Organization: C.U. srl News Server Lines: 37 Message-ID: <8737qitsov.fsf@debian.i-did-not-set--mail-host-address--so-tickle-me> References: NNTP-Posting-Host: 151.21.14.222 Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: virtdiesel.mng.cu.mi.it 1461021313 2298 151.21.14.222 (18 Apr 2016 23:15:13 GMT) X-Complaints-To: abuse@diesel.cu.mi.it NNTP-Posting-Date: Mon, 18 Apr 2016 23:15:13 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) Cancel-Lock: sha1:rQrftn65HJigYcZzdFMtE90qgas= Xref: csiph.com it.comp.lang.python:7642 MB339A writes: > #!/usr/bin/python > import os > > blocksize=500000 > > lenf=os.path.getsize("m2_1958.raw") > ncycles=lenf / blocksize > rest=lenf % blocksize > inf = open('m2_1958.raw', 'rb') > outf = open('m2_1958.wav', 'wb') > > for i in range (1,ncycles): > bread = inf.read(blocksize) > outf.write(bread) > print('*'), > inf.close() > outf.close() non è indispensabile sapere la lunghezza del file da copiare, il buffer che leggi è vuoto solo se hai già raggiunto la fine del file with open('m2_1958.raw', 'rb') as inf, open('m2_1958.wav', 'wb') as ouf: while True: buf = inf.read(blksz) if buf: ouf.write(buf) ; print('*', end='', flush=True) else: print() ; break se usi python 3 o usi `from __future__ import print_function` puoi usare degli argomenti opzionali per modificare in maniera autoevidente il comportamenteo di `print` se usi `with`, quando esci dal suo blocco i file aperti vengono chiusi automaticamente