简体   繁体   中英

How to access variable globally that have not been defined yet in python?

I am using python and tkinter to create a program that you can browse for an excel file that holds a list of file names to search a path for, If it finds the list of files it will allow the user to compress all the files into a copy zip file and save it I am pretty new with programming so i do apologize if this is rather simple. My issue is that I am trying to work with the list of files that is found in the Load_File function but when the program launches it has not been set by the user yet. I need to find a way to tell python to wait until the user browses to the excel file and once the file is set by the user set it globally so that I may use the list of files to search the OS. Below is my code:

def load_file():
    file_path = filedialog.askopenfilename(filetypes=[('Excel files', '*.xlsx')])
    wb = openpyxl.load_workbook(file_path)
    sheet = wb.active
    file_names = [cell.value for row in sheet.rows for cell in row]
    for file_name in file_names:
        listbox.insert('end', file_name)

def search_folder(folder, file_name):
    # Create an empty list to store the found file paths
    found_files = []
    for root, dirs, files in os.walk(folder):
        for file in files:
            if file in file_name:
                found_files.append(os.path.join(root, file))
    return found_files

folder = r'C:\Path\To\File'
found_files = search_folder(folder, file_names)

I have set file_name to global but as explained above it throws an error because the program has not yet received. Is there a way to get file_names into the search_folder function if I have to wait for the user to launch the program then browse to that file? I feel like the structure of my functions and tkniter window is wrong which is limiting my variable scope that I can work with. Any help would be much appreciated!

I think I get what your doing;
Create the list 'file_names' of the [file] names read from the selected (by askopenfilename) excel sheet. Then search the path 'folder' for each file in this list. If exist, prepend the 'folder' path to the name, saving into the list 'found_files'. The list box (at least for here is just for showing the file names found in the excel file.
So, the load_file function doesn't have a return for the list file_names, include that and call the function when calling the search_folder function.

def load_file():
    file_path = filedialog.askopenfilename(filetypes=[('Excel files', '*.xlsx')])
    wb = openpyxl.load_workbook(file_path)
    sheet = wb.active
    file_names = [cell.value for row in sheet.rows for cell in row]
    for file_name in file_names:
        listbox.insert('end', file_name)
    return file_names  # <--- return your list 


def search_folder(folder, file_name):
    # Create an empty list to store the found file paths
    found_files = []
    for root, dirs, files in os.walk(folder):
        for file in files:
            if file in file_name:
                found_files.append(os.path.join(root, file))
    return found_files

folder = r'C:\Path\To\File'
# found_files = search_folder(folder, file_names)
found_files = search_folder(folder, load_file())  # <-- call the load_file function, the return will be the list with the filenames.

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