tarfile

Dumping JSON directly into a tarfile

萝らか妹 提交于 2021-01-29 07:01:24
问题 I have a large list of dict objects. I would like to store this list in a tar file to exchange remotely. I have done that successfully by writing a json.dumps() string to a tarfile object opened in 'w:gz' mode. I am trying for a piped implementation, opening the tarfile object in 'w|gz' mode. Here is my code so far: from json import dump from io import StringIO import tarfile with StringIO() as out_stream, tarfile.open(filename, 'w|gz', out_stream) as tar_file: for packet in json_io_format

tarfile can't open tgz

十年热恋 提交于 2021-01-28 09:28:57
问题 I am trying to download tgz file from this website: https://plg.uwaterloo.ca/cgi-bin/cgiwrap/gvcormac/foo07 here is my script: import os from six.moves import urllib import tarfile spam_path=os.path.join('ML', 'spam') root_download='https://plg.uwaterloo.ca/cgi-bin/cgiwrap/gvcormac/foo07' spam_url=root_download+'255 MB Corpus (trec07p.tgz)' if not os.path.isdir(spam_path): os.makedirs(spam_path) path=os.path.join(spam_path, 'trec07p.tgz') if not os.path.isfile('trec07p.tgz'): urllib.request

tarfile模块之addfile方法

☆樱花仙子☆ 提交于 2020-05-09 12:19:49
tarfile模块打包文件时可以利用add()添加文件,同时也可以利用addfile()来添加内存中的二进制流。 很难说这样有什么用处,比如你想打包一个文件到包里面,想预先对文件进行修改,但是又不想创建临时文件。 zipfile也有对应的方法:writerstr,而且比tarfile简单。 TarFile.addfile()需要预先创建一个TarInfo对象,补充Tarinfo对象的名字,长度、时间等信息。 看看下面的例子 import time import tarfile from io import BytesIO test = 'what fuck you doing?\nare you ok?' tar = tarfile.open('test.tar.gz','w:gz') #构建Tarinfo对象,添加文件名、缓冲区长度、文件创建时间 info = tarfile.TarInfo(name = "test.txt") info.size = len(test) info.itime = time.time() #添加的IO文件必须是二进制形式 tar.addfile(info,BytesIO(test.encode('utf-8')) tar.close() 来源: oschina 链接: https://my.oschina.net/u/2434331/blog

Read Contents Tarfile into Python - “seeking backwards is not allowed”

给你一囗甜甜゛ 提交于 2020-01-05 04:01:08
问题 I am new to python. I am having trouble reading the contents of a tarfile into python. The data are the contents of a journal article (hosted at pubmed central). See info below. And link to tarfile which I want to read into Python. http://www.pubmedcentral.nih.gov/utils/oa/oa.fcgi?id=PMC13901 ftp://ftp.ncbi.nlm.nih.gov/pub/pmc/b0/ac/Breast_Cancer_Res_2001_Nov_9_3(1)_61-65.tar.gz I have a list of similar .tar.gz file I will eventually want to read in as well. I think (know) all of the tarfiles

Why tarfile module does not allow compressed appending?

99封情书 提交于 2019-12-24 08:47:09
问题 There is no straight way to append to a compressed tar archive. As the documentation states: Note that 'a:gz' , 'a:bz2' or 'a:xz' is not possible. As a workaround you can either use the uncompressed append mode 'a' and then handle the compression and decompression yourself, or you can handle the appending yourself and use the compressed read/write modes to recreate the tar archive. My question is: Why does it have to be this complicated? Is there any reason you can think of why the developers

Organizing files in tar bz2 file with python

霸气de小男生 提交于 2019-12-22 09:28:37
问题 I have about 200,000 text files that are placed in a bz2 file. The issue I have is that when I scan the bz2 file to extract the data I need, it goes extremely slow. It has to look through the entire bz2 file to fine the single file I am looking for. Is there anyway to speed this up? Also, I thought about possibly organizing the files in the tar.bz2 so I can instead have it know where to look. Is there anyway to organize files that are put into a bz2? More Info/Edit: I need to query the

Overwrite existing read-only files when using Python's tarfile

末鹿安然 提交于 2019-12-20 19:47:22
问题 I'm attempting to use Python's tarfile module to extract a tar.gz archive. I'd like the extraction to overwrite any target files it they already exist - this is tarfile's normal behaviour. However, I'm hitting a snitch in that some of the files have write-protection on (e.g. chmod 550). The tarfile.extractall() operation actually fails: IOError: [Errno 13] Permission denied '/foo/bar/file' If I try to delete the files from the normal command-line, I can do it, I just need to answer a prompt:

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

杀马特。学长 韩版系。学妹 提交于 2019-12-19 16:48:15
问题 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