用Python格式化一个文件

时间:2011-11-16 18:25:41

标签: 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)

谢谢!

7 个答案:

答案 0 :(得分:59)

有一个模块gzip。用法:

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

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

如何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()

编辑:

在Python> = 2.7中使用with

Jace Browning's answer显然更简洁易读,因此我的第二个代码段(并且应该)看起来像:

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)

答案 1 :(得分:30)

以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)

更短(在python 2.7.6上测试)

with open('path/to/file') as src, gzip.open('path/to/file.gz', 'wb') as dst:        
    dst.writelines(src)

答案 2 :(得分:17)

试试这个:

check_call(['gzip', fullFilePath])

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

答案 3 :(得分: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)

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

答案 4 :(得分:4)

关于此的文件实际上非常简单

如何读取压缩文件的示例:

import gzip
f = gzip.open('file.txt.gz', 'rb')
file_content = f.read()
f.close()

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

import gzip
content = "Lots of content here"
f = gzip.open('file.txt.gz', 'wb')
f.write(content)
f.close()

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

import gzip
f_in = open('file.txt', 'rb')
f_out = gzip.open('file.txt.gz', 'wb')
f_out.writelines(f_in)
f_out.close()
f_in.close()

https://docs.python.org/2/library/gzip.html

这就是整个文档。 。 。

答案 5 :(得分:2)

 //table pagination
        vm.totalItems = vm.customers.length;
        vm.currentPage = 1;
        vm.itemsPerPage = "5"; // default value
        vm.maxSize = 5; //Number of pager buttons to show
        vm.shownItems = vm.totalItems / vm.itemsPerPage;
        if(vm.shownItems < vm.totalItems)
            vm.shownItems = vm.totalItems;
        else
            vm.shownItems = vm.currentPage * vm.itemsPerPage;

        vm.setPage = function (pageNo) {
            vm.currentPage = pageNo;
        };

        vm.pageChanged = function() {
            var startPos = vm.currentPage * vm.itemsPerPage;
            console.log(startPos);
            vm.filteredCustomers = vm.customers.slice(startPos, startPos + vm.itemsPerPage);
        };

        vm.setItemsPerPage = function(num) {
            vm.itemsPerPage = num;
            vm.currentPage = 1; //reset to first page
        };

        // table ordering
        vm.propertyName = 'customerID';
        vm.reverse = true;

        vm.sortBy = function(propertyName) {
            vm.reverse = (vm.propertyName === propertyName) ? !vm.reverse : false;
            vm.propertyName = propertyName;
        };

答案 6 :(得分:2)

docs for 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)

创建一个新的gzip文件:

import gzip
content = b"Lots of content here"
with gzip.open("file.txt.gz", "wb") as f:
    f.write(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"))

有关其他编码方法的讨论,请参见this SO question