简体   繁体   English

如何在python中的特定目录中读取多个.gz文件而不解压缩它们

[英]how to read multiple .gz files in a particular directory in python without unzipping them

I have a folder /var/tmp in my linux directory where i have multiple .gz files in the below mentioned format (name_yyyymmddhhmmss.gz). 我的linux目录中有一个文件夹/ var / tmp,其中有多个.gz文件,它们的格式如下(name_yyyymmddhhmmss.gz)。

aakashdeep_20181120080005.gz aakashdeep_20181120080025.gz kalpana_20181119080005.gz aakashdeep_20181120080025.gz aakashdeep_20181120080005.gz aakashdeep_20181120080025.gz kalpana_20181119080005.gz aakashdeep_20181120080025.gz

Now i want to open all the gz files with format as name_20181120*.gz without unzipping them and read the content out of them. 现在我想打开所有格式为name_20181120 * .gz的gz文件,而不将其解压缩并从中读取内容。

i have written a simple code 我写了一个简单的代码

!/usr/bin/python !在/ usr / bin中/蟒蛇

import gzip 导入gzip

output = gzip.open('/var/tmp/Aakashdeep/aakashdeep_20181120080002.gz','r') 输出= gzip.open('/ var / tmp / Aakashdeep / aakashdeep_20181120080002.gz','r')

for line in output: print (line) 对于输出中的行:打印(行)

and the same is giving me the output as expected, but i want to open all the files like below output = gzip.open('/var/tmp/Aakashdeep/aakashdeep_20181120*.gz','r') 并且同样给了我预期的输出,但是我想打开以下输出= gzip.open('/ var / tmp / Aakashdeep / aakashdeep_20181120 * .gz','r')的所有文件

Can anyone suggest me the way for this.?? 谁能建议我这样做的方式。?

Use glob.glob to obtain a list of files to process, then open each with gzip.open , do something with its contents, and move on to the next. 使用glob.glob获取要处理的文件列表,然后使用gzip.open打开每个文件,对其内容进行处理,然后移至下一个。 Outline (untested): 大纲(未试用):

import glob
import gzip

ZIPFILES='/var/tmp/Aakashdeep/aakashdeep_20181120*.gz'

filelist = glob.glob(ZIPFILES)
for gzfile in filelist:
    # print("#Starting " + gzfile)  #if you want to know which file is being processed  
    with gzip.open( gzfile, 'r') as f:
        for line in f:
             print(line)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Python 将多个 .gz 文件解压缩为单个文本文件 - Unzipping multiple .gz files into single text file using Python 如何通过 csv 文件中的块在巨大的 tar.gz 中获取 pandas dataframe 而不解压缩和迭代它们? - How to get pandas dataframe by chunks from csv files in huge tar.gz without unzipping and iterating over them? 如何从一个目录中读取多个文件并在 python 中读取 append 它们? - How to read multiple files from a directory and append them in python? 如何在不解压缩内容的情况下查看.tar.gz存档中特定文件的内容? - how to see the content of a particular file in .tar.gz archive without unzipping the contents? 您如何(逐行)读取 Python 中压缩文件夹内的多个.gz 文件而不创建临时文件? - How do you (line by line) read multiple .gz files that are inside a zipped folder in Python without creating temporary files? 解压缩目录中的多个zip文件? - Unzipping multiple zip files in a directory? 如何在不解压缩的情况下读取gzip格式的文本文件,使用python将那行写入excel? - How to read the text files in gzip format without unzipping it write that line to excel using python? Python:读取多个文件并将其根据其内容移动到目录 - Python: Read multiple files and move them to a directory according to their content 如何从单个目录中读取多个csv文件并在Python中单独绘制它们? - How can I read multiple csv files from a single directory and graph them separately in Python? Python脚本读取一个目录中的多个excel文件并将它们转换为另一个目录中的.csv文件 - Python script to read multiple excel files in one directory and convert them to .csv files in another directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM