GZip文件夹

时间:2018-03-20 19:59:46

标签: python gzip

我编译了一个可以打开.GZ文件并将其解压缩到.txt文件的代码 以下代码可以使用

import os       
import gzip
import shutil
extension = ".gz"
dir_name = 'C:\\test'
dest_name= 'C:\\test\Extract'
os.chdir(dir_name) # change directory from working dir to dir with files

for item in os.listdir(dir_name): # loop through items in dir
    if item.endswith(extension): # check for ".gz" extension
         file_name = os.path.abspath(item) # get full path of files
         with gzip.open(file_name, 'rb') as f_in:
                 with open(file_name+ '.txt', 'wb') as f_out:
                     shutil.copyfileobj(f_in, f_out)

但代码写在同一目录中,我无法在以下行中添加dest_name

with open(file_name+ '.txt', 'wb') as f_out:

1 个答案:

答案 0 :(得分:0)

你需要剥离" .gz"在添加扩展名之前,从文件名中将其加入dest_name。更改工作目录通常是不受欢迎的,因为它对程序是全局的,并且它可能会混淆您随时间引用的目录。 glob模块适用于简单文件匹配,并返回文件的绝对路径或相对路径。

import os       
import gzip
import shutil
from glob import glob

extension = ".gz"

# issue: backslash is python's string escape character. You can get the
# backslashes with raw strings (r'c:\\test') or double escaping ('c:\\\\test'),
# or just use a forward slash and let python figure it out ('c://test').

dir_name = r'C:\\test'
dest_name= r'C:\\test\Extract'

# issue: glob is well-suited for listing files like this. It returns a full relative
# path to the file. chdir is frowned apon because it is global to the program.

for file_name in glob(os.path.join(dir_name, extension)): # loop through items in dir
     with gzip.open(file_name, 'rb') as f_in:
         # issue: strip off the .gz extension and add the dest folder
         base_name = os.path.basename(file_name)
         new_name = os.path.splitext(base_name)[0] + '.txt'
         with open(os.path.join(dest_name, new_name), 'wb') as f_out:
             shutil.copyfileobj(f_in, f_out)