如何从Python文件夹中解压缩多个zip文件夹

时间:2019-07-24 21:02:21

标签: python python-2.7

我有一个可以提取.txt文件并将python shell输出到名为“ output.txt”的文件的代码。我一直试图找到一种方法来解压缩多个zip文件夹,以便我的代码可以遍历其中的文件,但是我找不到如何解压缩多个zip文件夹的方法。我尝试使用扩展名.endswith(“。zip”),但是它不起作用(它不在我的代码中)。第一个功能可能是问题,我可能是解压缩.zip文件的错误方法。知道如何编码吗?谢谢,我一直在尝试在我的代码中实现它,但仍然没有成功。

import re
import os
from zipfile import ZipFile
import glob

# I have tried this but doesn't work 
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)
def pain():

    print("\t\t\tinput_files.zip has been unzipped")



    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
# I am thinking this for loop might be the problem because
# it is specifically looking for a file
# called "file.txt" in the folder instead of going through the
# unzipped folder
    #for filename in os.listdir(os.getcwd() + "/input_files"):
    for filename in os.listdir('C:\Users\M29858\Desktop\TestPy\Version13'):
        with open(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__':
    main()
    pain()


#THIS WORKS 

回溯错误:

Traceback (most recent call last):
  File "C:\Users\M29858\Desktop\TestPy\Version13\letseeifitworks.py", line 74, in <module>
    main()
  File "C:\Users\M29858\Desktop\TestPy\Version13\letseeifitworks.py", line 18, in main
    archive.extract('file.txt', path=dirname)
  File "C:\Program Files (x86)\Python27\lib\zipfile.py", line 945, in extract
    member = self.getinfo(member)
  File "C:\Program Files (x86)\Python27\lib\zipfile.py", line 857, in getinfo
    'There is no item named %r in the archive' % name)
KeyError: "There is no item named 'file.txt' in the archive"

预期:

input_files.zip has been unzipped
Filename    Line    number of numbers   string separated by a comma white space found   tab found   carriage return found
file.txt        1       3                       0,3,4                           False                   False           True                 
file.txt        2       3                       0,0,1                           True                    True            True                 
file.txt        3       3                       5,0,1                           False                   False           False (end of file)
other_files.zip has been unzipped
Filename    Line    number of numbers   string separated by a comma white space found   tab found   carriage return found
file.txt        1       3                       0,3,4                           False                   False           True                 
file.txt        2       3                       0,0,1                           True                    True            True                 
file.txt        3       3                       5,0,1                           False                   False           False (end of file)
more_files.zip has been unzipped
Filename    Line    number of numbers   string separated by a comma white space found   tab found   carriage return found
file.txt        1       3                       0,3,4                           False                   False           True                 
file.txt        2       3                       0,0,1                           True                    True            True                 
file.txt        3       3                       5,0,1                           False                   False           False (end of file)

实际:

input_files.zip has been unzipped
Filename    Line    number of numbers   string separated by a comma white space found   tab found   carriage return found

0 个答案:

没有答案
相关问题