Ulr*_*rok 5 python compression tarfile
我注意到 tarfile 没有 aw:xz 选项或类似的选项,有没有办法创建 xz 文件?我在 python 中有这个代码
dir=tkFileDialog.askdirectory(initialdir="/home/david")
if x.get()=="gz":
tar = tarfile.open(dir+".tar.gz", "w:gz")
tar
for i in range(lbox.size()):
tar.add(e1.get()+"/"+lbox.get(i),arcname=lbox.get(i))
tar.close()
if x.get()=="bz":
tar = tarfile.open(dir+".tar.gz", "w:bz2")
tar
for i in range(lbox.size()):
tar.add(e1.get()+"/"+lbox.get(i),arcname=lbox.get(i))
tar.close()
if x.get()=="xz":
tar = tarfile.open(dir+".tar.gz", "w:gz")
tar
for i in range(lbox.size()):
tar.add(e1.get()+"/"+lbox.get(i),arcname=lbox.get(i))
tar.close()
Run Code Online (Sandbox Code Playgroud)
小智 6
Python 3.3 及更高版本具有您正在搜索的选项。
'w:xz' -- 打开 lzma 压缩写入。
https://docs.python.org/3.3/library/tarfile.html
对于 3.3 以下的版本,您可以尝试以下操作
示例代码:
import lzma
# open input file as binary and read input data
with open(inputFilename, 'rb') as iFile:
iData = iFile.read()
# compress data
oData = lzma.compress(iData)
# open output file as binary and write compressed data
with open(outputFilename, 'wb') as oFile:
oFile.write(oData)
Run Code Online (Sandbox Code Playgroud)
我搜索了其他答案,发现一个条目提到了将 lzma 导入 python 2.7 的问题。此条目中提供了一种解决方法,您可以遵循。
这是链接 - Python 2.7:使用“lzma”模块压缩 XZ 格式的数据
| 归档时间: |
|
| 查看次数: |
2931 次 |
| 最近记录: |