Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > it.comp.lang.python > #7642

Re: Copiare file "a pezzi"

From pecore@pascolo.org
Newsgroups it.comp.lang.python
Subject Re: Copiare file "a pezzi"
Date 2016-04-19 01:15 +0200
Organization C.U. srl News Server
Message-ID <8737qitsov.fsf@debian.i-did-not-set--mail-host-address--so-tickle-me> (permalink)
References <ncpmig$12kr$1@gioia.aioe.org>

Show all headers | View raw


MB339A <Freccetricolori@tin.it> 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

Back to it.comp.lang.python | Previous | NextPrevious in thread | Find similar


Thread

Copiare file "a pezzi" MB339A <Freccetricolori@tin.it> - 2016-03-21 21:46 +0100
  Re: Copiare file "a pezzi" MB339A <Freccetricolori@tin.it> - 2016-03-21 22:29 +0100
  Re: Copiare file "a pezzi" pecore@pascolo.org - 2016-04-19 01:15 +0200

csiph-web