[英]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 我写了一个简单的代码
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.