EDIT 1is_tarfile() restituisce True per un file vuoto
Hmm, accetto le risposte che tar rispetta un file vuoto ... ma sul mio sistema:
$ touch emptytar
$ tar -tf emptytar
tar: This does not look like a tar archive
tar: Exiting with failure status due to previous errors
Forse ho una versione non canonica?
$ tar --version
tar (GNU tar) 1.22
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by John Gilmore and Jay Fenlason.
Ciao a tutti,
sto testando una logica per gestire un utente caricamento di un file TAR. Quando mi nutro un file vuoto per tarfile.is_tarfile()
ritorna True
, che non è quello che mi aspetto:
$ touch tartest
$ cat tartest
$ python -c "import tarfile; print tarfile.is_tarfile('tartest')"
True
Se posso aggiungere del testo al file, restituisce False
, che mi aspetto:
$ echo "not a tar" > tartest
$ python -c "import tarfile; print tarfile.is_tarfile('tartest')"
False
potrei aggiungere un controllo all'inizio per verificare la presenza di un file di lunghezza zero, ma sulla base del documentation per tarfile.is_tarfile(name)
Penso che questo sia unecessary:
Restituisce True se il nome è un file tar , che il modulo tarfile può leggere .
sono andato al punto di controllare la fonte, tarfile.py, e posso vedere che sta controllando i blocchi di intestazione ma non comprendere appieno come si sta valutando quei blocchi.
Sto fraintendendo la documentazione e quindi ponendo delle aspettative ingiuste?
Grazie,
Zachary
Il 'tar' che ho provato con GNU è 1.15.1 (come portato da Apple a MacOSX/Darwin); Non riesco a spiegare la tua osservazione sul fatto che 1.22 sembra essere cambiata in un modo incompatibile all'indietro! –
Posso confermare il comportamento di errore di GNU tar 1.26 su un file vuoto: genera un errore mentre tarfile.is_tarfile restituisce True – m13r