简体   繁体   中英

How to find a zip file, unzip it, find a specific file in it using python?

I need to 1) Find a zipfile at a particular directory location 2) If it exists then unzip it 3) Out of its contents find a specific file and move it to other directory.

def searchfile():
for file in os.listdir('/user/adam/datafiles'):
    if fnmatch.fnmatch(file, 'abc.zip'):
        return True
return False

if searchfile():

print('File exists')

else:

print('File not found')

def file_extract():

    os.chdir('/user/adam/datafiles')
    file_name = 'abc.zip'
    destn = '/user/adam/extracted_files'
    zip_archive = ZipFile (file_name)
    zip_archive.extract('class.xlsx',destn)
    print("Extracted the file")
    zip_archive.close()

search_file

file_extract

When I execute the above script, it shows no compile time issues or runtime issues,. but it just works for the first function. When I check for the files in the extracte_files folder I don't see the files.

The only place you define found is in the if block, so if abc.zip is not found, found will be undefined. But moreover, even if abc.zip is found and found is defined, it is defined as a local variable to searchfile() , and your main scope won't have access to it. You should initialize it as a global variable in the main scope, and declare it as a global variable in searchfile() so that modifications to it can be reflected in the main scope:

def searchfile():
    global found
    for file in os.listdir('/user/adam/datafiles'):
        if fnmatch.fnmatch(file, 'abc.zip'):
            found = True

found = False
searchfile()
if found:
    print('File exists')
else:
    print('File not found')

But the use of global variable really isn't necessary since you can simply return found as a returning value from searchfile() :

def searchfile():
    for file in os.listdir('/user/adam/datafiles'):
        if fnmatch.fnmatch(file, 'abc.zip'):
            return True
    return False

if searchfile():
    print('File exists')
else:
    print('File not found')

Note that you have never actually called searchfile() , and even if you had, found would still not be defined if abc.zip is not matched.

If you want to have file searching in a separate function (which is a good idea), you may be better off having it return a success/failure boolean instead of relying on a global variable.

So you may want something like this: (Note: code not tested)

import os
import fnmatch
import zipfile

def searchfile():
        for file in os.listdir('/user/adam/datafiles'):
                if fnmatch.fnmatch(file, 'abc.zip'):
                        return True  # <-- Note this
        return False  # <-- And this

if searchfile():  # <-- Now call the function and use its return value
        print('File exists')
else:
        print('File not found')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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