解压缩目录中的多个zip文件?

时间:2013-12-10 12:20:48

标签: python python-2.7 zip gz

我需要在目录中解压缩多个文件并将它们转换为csv文件。 文件在文件中按顺序编号,1.gz,2.gz,3.gz等

这可以在一个脚本中完成,还是我必须手动完成?

编辑:当前代码是

 #! /usr/local/bin/python

import gzip
import csv
import os

f = gzip.open('1.gz', 'rb')
file_content = f.read()
filename = '1.txt'
target = open ('1.txt', 'w')
target.write(file_content)
target.close()
filename = '1.csv'
txt_file = '1.txt'
csv_file = '1.csv'
in_txt = csv.reader(open(txt_file, "rb"), delimiter = '\t')
out_csv = csv.writer(open(csv_file, 'wb'))
out_csv.writerows(in_txt)
dirname = '/home/user/Desktop'
filename = "1.txt"
pathname = os.path.abspath(os.path.join(dirname, filename))
if pathname.startswith(dirname):
os.remove(pathname)
f.close()

目前的计划是对每个目录的.gz文件总数进行计数,并为每个文件使用循环解压缩并打印出txt / csv。

是否可行或有更好的方法吗?

此外,python是否类似于perl,其中双引号解释字符串?

1 个答案:

答案 0 :(得分:0)

你几乎不需要Python :))

但是可以在单个Python脚本中执行此操作。你需要使用:

  • OS
  • os.path(可能
  • gzip的
  • glob(将获得一个很好的文件列表。例如:glob("*.gz")

https://docs.python.org/上阅读这些模块并快速完成! :)

相关问题