1
私のシステムには10個のzipファイルを含むディレクトリがあります。各zipファイルには1つのテキストファイルが含まれています。私は、ディレクトリ内のすべてのファイルを解凍し、結果の(解凍された)すべてのファイルを1つのファイルに連結するPythonスクリプトを作成したいと思います。これどうやってするの?これまでは、すべてのファイルを解凍しているスクリプトがありますが、連結を追加する方法についてはわかりません。以下は私が持っているものです。あなたはそれらを解凍したときに、ディスクにファイルを記述する必要はありませんPythonファイルを解凍して結果を連結
import os, zipfile
dir_name = '/path/to/dir'
pattern = "my-pattern*.gz"
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 == pattern: # check for my pattern extension
file_name = os.path.abspath(item) # get full path of files
zip_ref = zipfile.ZipFile(file_name) # create zipfile object
zip_ref.extractall(dir_name) # extract file to dir
zip_ref.close() # close file
するhttp://stackoverflow.com/questions/5509872で多くの例の回答を利用することができるようになります。だから、あなたが連結された結果以外のものを必要としないと仮定すると、あなたとの最後の2行を置き換えます/ python-append-multiple-files-in-given-order-to-one-big-file – dbmitch