[issue44262] tarfile: some content different output

Filipe Laíns report at bugs.python.org
Sun May 30 12:55:10 EDT 2021


Filipe Laíns <lains at riseup.net> added the comment:

tarfile will keep the mtime from the file, the issue is that you are touching the files in the beginning of the script. When you write to the files, you change the mtime (last modified time), which produces a different TarInfo. If you comment out the code that writes to the files, you get the exact same output.


#dir0 = Path("/tmp/a")
#dir0.mkdir(parents=True, exist_ok=True)
#fil0 = dir0 / "eph0"
#fil0.write_text("Text 0", encoding="UTF-8")
#fil1 = dir0 / "eph1"
#fil1.write_text("Text 1", encoding="UTF-8")
#fil2 = dir0 / "eph2"
#fil2.write_text("Text 2", encoding="UTF-8")


$ python compress.py
b'cc3bd1bf99edc4f0796e1c466d251b0f808db790cbdd55bc920c041fb405e535  /tmp/py_gzip.tgz\n'
b'cc3bd1bf99edc4f0796e1c466d251b0f808db790cbdd55bc920c041fb405e535  /tmp/py_gzip.tgz\n'
$ python compress.py
b'cc3bd1bf99edc4f0796e1c466d251b0f808db790cbdd55bc920c041fb405e535  /tmp/py_gzip.tgz\n'
b'cc3bd1bf99edc4f0796e1c466d251b0f808db790cbdd55bc920c041fb405e535  /tmp/py_gzip.tgz\n'


If you are in a situation where the mtime may change, but you want the same output, you can reset it. See the last example in https://docs.python.org/3/library/tarfile.html#tar-examples.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44262>
_______________________________________


More information about the Python-bugs-list mailing list