用Python格式化一个文件

Rin*_*nks 32 python gzip subprocess

我想用Python gzip文件.我试图使用subprocss.check_call(),但它一直失败,错误'OSError:[Errno 2]没有这样的文件或目录'.我在这里尝试的是否有问题?有没有比使用subprocess.check_call更好的gzip文件的方法?

from subprocess import check_call

def gZipFile(fullFilePath)
    check_call('gzip ' + fullFilePath)
Run Code Online (Sandbox Code Playgroud)

谢谢!!

Xae*_*ess 63

有一个模块gzip.用法:

如何创建压缩GZIP文件的示例:

import gzip
content = b"Lots of content here"
f = gzip.open('/home/joe/file.txt.gz', 'wb')
f.write(content)
f.close()
Run Code Online (Sandbox Code Playgroud)

如何GZIP压缩现有文件的示例:

import gzip
f_in = open('/home/joe/file.txt')
f_out = gzip.open('/home/joe/file.txt.gz', 'wb')
f_out.writelines(f_in)
f_out.close()
f_in.close()
Run Code Online (Sandbox Code Playgroud)

编辑:

Jace Browningwith在Python> = 2.7中使用的答案显然更简洁易读,因此我的第二个片段(并且应该)看起来像:

import gzip
with open('/home/joe/file.txt', 'rb') as f_in, gzip.open('/home/joe/file.txt.gz', 'wb') as f_out:
    f_out.writelines(f_in)
Run Code Online (Sandbox Code Playgroud)

  • @Benoît:由于输出文件的名称与正在读取的文件名称不同,很明显它没有这样做。这样做需要将压缩数据临时存储在其他地方,直到原始文件中的所有数据都被压缩。 (2认同)

Jac*_*ing 31

在Python 2.7格式中:

import gzip

with open("path/to/file", 'rb') as orig_file:
    with gzip.open("path/to/file.gz", 'wb') as zipped_file:
        zipped_file.writelines(orig_file)
Run Code Online (Sandbox Code Playgroud)

更短(在Python 2.7.6上测试)

with open('path/to/file') as src, gzip.open('path/to/file.gz', 'wb') as dst:        
    dst.writelines(src)
Run Code Online (Sandbox Code Playgroud)


ret*_*ile 19

试试这个:

check_call(['gzip', fullFilePath])
Run Code Online (Sandbox Code Playgroud)

根据您对这些文件的数据所做的操作,Skirmantas链接到http://docs.python.org/library/gzip.html也可能会有所帮助.请注意页面底部附近的示例.如果您不需要访问数据,或者您的Python代码中没有数据,那么执行gzip可能是最简单的方法,因此您不必在Python中处理数据.


Mic*_*all 13

来自Python3文档

Gzip 现有文件

import gzip
import shutil
with open('file.txt', 'rb') as f_in:
    with gzip.open('file.txt.gz', 'wb') as f_out:
        shutil.copyfileobj(f_in, f_out)

# or because I hate nested with statements

import gzip
import shutil
from contextlib import ExitStack
with ExitStack() as stack:
    f_in = stack.enter_context(open('file.txt', 'rb'))
    f_out = stack.enter_context(gzip.open('file.txt.gz', 'wb'))
    shutil.copyfileobj(f_in, f_out)
Run Code Online (Sandbox Code Playgroud)

创建一个新的 gzip 文件:

import gzip
content = b"Lots of content here"
with gzip.open("file.txt.gz", "wb") as f:
    f.write(content)
Run Code Online (Sandbox Code Playgroud)

注意content变成字节的事实

如果您不像上面的例子那样将内容创建为字符串/字节文字,另一种方法是

import gzip
# get content as a string from somewhere else in the code
with gzip.open("file.txt.gz", "wb") as f:
    f.write(content.encode("utf-8"))
Run Code Online (Sandbox Code Playgroud)

有关其他编码方法的讨论,请参阅此 SO 问题


cho*_*own 5

使用gzip模块:

import gzip
import os

in_file = "somefile.data"
in_data = open(in_file, "rb").read()
out_gz = "foo.gz"
gzf = gzip.open(out_gz, "wb")
gzf.write(in_data)
gzf.close()

# If you want to delete the original file after the gzip is done:
os.unlink(in_file)
Run Code Online (Sandbox Code Playgroud)

您的错误:OSError: [Errno 2] No such file or directory'告诉您该文件fullFilePath不存在.如果您仍然需要走这条路线,请确保您的系统上存在该文件,并且您使用的是绝对路径而非相对路径.