繁体   English   中英

如何解压缩所有以.zip结尾的文件夹/文件,并从每个压缩的文件夹中提取“ file.txt”文件

[英]How to unzip all folders/files that end in .zip and extract “file.txt” file from each zipped folder

我的代码当前将一个zip文件夹解压缩,并找到名为file.txt的文件并将其解压缩。 现在,我需要解压缩多个扩展名为.zip的文件夹。 我试图使用类似于我需要执行的代码,但问题是现在我必须在每个.zip文件夹中找到一个名为file.txt文件,然后仅提取该文件。 也可以将file.txt存储到一个单独的具有相同名称的文件夹中。 预先感谢您的宝贵时间。

    import re
    import os
    from zipfile import ZipFile
    def pain():

    print("\t\t\tinput_files.zip has been unzipped")
    with ZipFile('input_files.zip', 'r') as zipObj:
        zipObj.extractall()
        listOfFileNames = zipObj.namelist()
        for fileName in listOfFileNames:
            if fileName.endswith('.txt'):
                zipObj.extract(fileName, 'storage')


    outfile = "output2.txt"                 #this will be the filename that the code will write to 
    baconFile = open(outfile,"wt")
    file_name1 = "file.txt"
    print('Filename\tLine\tnumber of numbers\tstring separated by a comma\twhite space found\ttab found\tcarriage return found\n')         #This prints the master column in the python shell and this is the way the code should collect the data 
    baconFile.write('Filename\tLine\tnumber of numbers\tstring separated by a comma\twhite space found\ttab found\tcarriage return found\n') #This prints the master column in the output file and this is the way the code should collect the data

    #for filename in os.listdir(os.getcwd() + "/input_files"):
    for filename in os.listdir('C:\Users\M29858\Desktop\TestPy\Version10\input_files'):
        with open("input_files/" + filename, 'r') as f:
            if file_name1 in filename:

                output_contents(filename, f, baconFile)
    baconFile.close()       #closes the for loop that the code is writing to


    def output_contents(filename, f, baconFile):     #using open() function to open the file inside the directory
        index = 0
        for line in f:
                                        #create a list of all of the numerical values in our line
            content = line.split(',')       #this will be used to count the amount numbers before and after comma
            whitespace_found = False
            tab_found = False
            false_string = "False (end of file)"
            carriage_found = false_string 
            sigfigs = ""

            index += 1                            #adds 1 for every line if it finds what the command wants

        if " " in line:                         #checking for whitespace
            whitespace_found = True
        if "\t" in line:                        #checking for tabs return
            tab_found = True
        if '\n' in line:                    #checking if there is a newline after the end of each line
            carriage_found = True                                        
        sigfigs = (','.join(str(len(g)) for g in re.findall(r'\d+\.?(\d+)?', line )))    #counts the sigsfigs after decimal point 

        print(filename + "\t{0:<4}\t{1:<17}\t{2:<27}\t{3:17}\t{4:9}\t{5:21}"
              .format(index, len(content), sigfigs, str(whitespace_found), str(tab_found), str(carriage_found)))   #whatever is inside the .format() is the way it the data is stored into
        baconFile.write('\n')
        baconFile.write( filename + "\t{0:<4}\t{1:<17}\t{2:<27}\t{3:17}\t{4:9}\t{5:21}"
                        .format(index, len(content), sigfigs, str(whitespace_found), str(tab_found), str(carriage_found)))



if __name__ == '__main__':
    pain()


#THIS WORKS 
import glob
import os
from zipfile import ZipFile

def main():
    for fname in glob.glob("*.zip"):  # get all the zip files
        with ZipFile(fname) as archive:
            # if there's no file.txt, ignore and go on to the next zip file
            if 'file.txt' not in archive.namelist(): continue

            # make a new directory named after the zip file
            dirname = fname.rsplit('.',1)[0]
            os.mkdir(dirname)

            extract file.txt into the directory you just created
            archive.extract('file.txt', path=dirname)

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM