tarfile

How to append a file to a tar file use python tarfile module?

拈花ヽ惹草 提交于 2019-12-05 13:07:13
问题 I want to append a file to the tar file. For example, the files in test.tar.gz are a.png, b.png, c.png . I have a new png file named a.png , I want to append to a.png to test.tar.gz and cover the old file a.png in test.tar.gz . My code: import tarfile a = tarfile.open('test.tar.gz', 'w:gz') a.add('a.png') a.close() then, all the files in test.tar.gz disappeard but a.png , if I change my code to this: import tarfile a = tarfile.open('test.tar.gz', 'a:')# or a:gz a.add('a.png') a.close() the

How to append a file to a tar file use python tarfile module?

我与影子孤独终老i 提交于 2019-12-04 00:10:26
I want to append a file to the tar file. For example, the files in test.tar.gz are a.png, b.png, c.png . I have a new png file named a.png , I want to append to a.png to test.tar.gz and cover the old file a.png in test.tar.gz . My code: import tarfile a = tarfile.open('test.tar.gz', 'w:gz') a.add('a.png') a.close() then, all the files in test.tar.gz disappeard but a.png , if I change my code to this: import tarfile a = tarfile.open('test.tar.gz', 'a:')# or a:gz a.add('a.png') a.close() the program is crashed, error log: Traceback (most recent call last): File "<stdin>", line 1, in <module>

How can I process a tarfile with a Python multiprocessing pool?

霸气de小男生 提交于 2019-12-03 09:53:35
问题 I'm trying to process the contents of a tarfile using multiprocessing.Pool . I'm able to successfully use the ThreadPool implementation within the multiprocessing module, but would like to be able to use processes instead of threads as it would possibly be faster and eliminate some changes made for Matplotlib to handle the multithreaded environment. I'm getting an error that I suspect is related to processes not sharing address space, but I'm not sure how to fix it: Traceback (most recent

How can I process a tarfile with a Python multiprocessing pool?

半城伤御伤魂 提交于 2019-12-03 00:28:00
I'm trying to process the contents of a tarfile using multiprocessing.Pool . I'm able to successfully use the ThreadPool implementation within the multiprocessing module, but would like to be able to use processes instead of threads as it would possibly be faster and eliminate some changes made for Matplotlib to handle the multithreaded environment. I'm getting an error that I suspect is related to processes not sharing address space, but I'm not sure how to fix it: Traceback (most recent call last): File "test_tarfile.py", line 32, in <module> test_multiproc() File "test_tarfile.py", line 24,

Python: tarfile stream

社会主义新天地 提交于 2019-12-01 22:55:42
问题 I would like to read some files from a tarball and save it to a new tarball. This is the code I wrote. archive = 'dum/2164/archive.tar' # Read input data. input_tar = tarfile.open(archive, 'r|') tarinfo = input_tar.next() input_tar.close() # Write output file. output_tar = tarfile.open('foo.tar', 'w|') output_tar.addfile(tarinfo) output_tar.close() Unfortunately, the output tarball is no good: $ tar tf foo.tar ./1QZP_A--2JED_A--not_reformatted.dat.bz2 tar: Truncated input file (needed 1548288

Python: tarfile stream

微笑、不失礼 提交于 2019-12-01 22:21:26
I would like to read some files from a tarball and save it to a new tarball. This is the code I wrote. archive = 'dum/2164/archive.tar' # Read input data. input_tar = tarfile.open(archive, 'r|') tarinfo = input_tar.next() input_tar.close() # Write output file. output_tar = tarfile.open('foo.tar', 'w|') output_tar.addfile(tarinfo) output_tar.close() Unfortunately, the output tarball is no good: $ tar tf foo.tar ./1QZP_A--2JED_A--not_reformatted.dat.bz2 tar: Truncated input file (needed 1548288 bytes, only 1545728 available) tar: Error exit delayed from previous errors. Any clue how to read and

How to unpack xz file with python which contains only data but no filename?

三世轮回 提交于 2019-12-01 16:21:58
I have a file, which I can decompress under linux using the following command: unxz < file.xz > file.txt How can I do the same using python? If I use python3 and the tarfile module and do the following: import sys import tarfile try: with tarfile.open('temp.xz', 'r:xz') as t: t.extract() except Exception as e: print("Error:", e.strerror) I get the exception: ReadError('invalid header',) . So apparently it expects some file- or directory information which is not present in the xz file. So how can I decompress a file without header information? The tarfile module is only for... err... tar files.

Read contents of .tar.gz file from website into a python 3.x object

老子叫甜甜 提交于 2019-12-01 07:12:30
问题 I am new to python. I can't figure out what I am doing wrong when trying to read the contents of .tar.gz file into python. The tarfile I would like to read is hosted at the following web address: ftp://ftp.ncbi.nlm.nih.gov/pub/pmc/b0/ac/Breast_Cancer_Res_2001_Nov_9_3(1)_61-65.tar.gz more info on file at this site (just so you can trust contents) http://www.pubmedcentral.nih.gov/utils/oa/oa.fcgi?id=PMC13901 The tarfile contains .pdf and .nxml copies of the journal article. And also a couple of

Safely extract zip or tar using Python

混江龙づ霸主 提交于 2019-11-30 05:58:32
I'm trying to extract user-submitted zip and tar files to a directory. The documentation for zipfile's extractall method (similarly with tarfile's extractall ) states that it's possible for paths to be absolute or contain .. paths that go outside the destination path. Instead, I could use extract myself, like this: some_path = '/destination/path' some_zip = '/some/file.zip' zipf = zipfile.ZipFile(some_zip, mode='r') for subfile in zipf.namelist(): zipf.extract(subfile, some_path) Is this safe? Is it possible for a file in the archive to wind up outside of some_path in this case? If so, what

Determine whether any files have been added, removed, or modified in a directory

痴心易碎 提交于 2019-11-30 04:01:17
问题 I'm trying to write a Python script that will get the md5sum of all files in a directory (in Linux). Which I believe I have done in the code below. I want to be able to run this to make sure no files within the directory have changed, and no files have been added for deleted. The problem is if I make a change to a file in the directory but then change it back. I get a different result from running the function below. (Even though I changed the modified file back. Can anyone explain this. And