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

时间:2019-07-23 19:42:01

标签: python python-2.7

我的代码当前解压缩一个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 

1 个答案:

答案 0 :(得分:0)

edge = [[0,2,2],[2,3,2],[3,1,1],[1,0,4],[1,2,3],[0,3,4],[3,0,5]]
# Here n is the value of the highest numbered-vertex. In the above graph, n=4
n=4
next_node = floyd_warshall(n,edge)
for i in range(4):
    for j in range(4):
        for path in all_paths(next_node, i, j):
            print((i, j, path))