Search code examples
pythonfunctionvariablesgloballocal

Python: Use local variable globally does not work


I am trying to use my code to open a file after searching for it in either operating system. However when I assign the variable inside the function, i cant use it outside of the function. And when I keep the 2nd function out of 1st function, it doesnt recognize the function.

I tried to assign the df_location globally, but this doesnt work. When i use df = pd.read_csv(df_location[0], index_col=0) inside the function, I am not able to use df anywhere else in my code.

if platform.system() == 'windows':
    def find_file(root_folder, rex):
        for root, dirs, files in os.walk(root_folder):
            for f in files:
                result = rex.search(f)
                if result:
                    file_path = os.path.join(root, f)
                    return file_path  

    def find_file_in_all_drives(file_name):

        matching_files = list()
        # create a regular expression for the file
        rex = re.compile(file_name)
        for drive in win32api.GetLogicalDriveStrings().split('\000')[:-1]:
            file_path = find_file(drive, rex)
            if file_path:
                matching_files.append(file_path)
        return matching_files

    global df_location
    df_location = find_file_in_all_drives("AB_NYC_2019.csv")

if platform.system() == 'mac':
    df_location = find_file("/", "AB_NYC_2019.csv")


df = pd.read_csv(df_location[0], index_col=0)

I would like to be able to use the file that is retrieved through the functions.

Thank you!

ideally it should be like this

if platform.system() == 'windows':
    def find_file(root_folder, rex):
        for root, dirs, files in os.walk(root_folder):
            for f in files:
                result = rex.search(f)
                if result:
                    file_path = os.path.join(root, f)
        return file_path  

    def find_file_in_all_drives(file_name):

        matching_files = list()
        # create a regular expression for the file
        rex = re.compile(file_name)
        for drive in win32api.GetLogicalDriveStrings().split('\000')[:-1]:
            file_path = find_file(drive, rex)
            if file_path:
                matching_files.append(file_path)
        return matching_files

df_location = find_file_in_all_drives("AB_NYC_2019.csv")

if platform.system() == 'mac':
    df_location = find_file("/", "AB_NYC_2019.csv")


df = pd.read_csv(df_location[0], index_col=0)

but this gives the error message: "NameError: name 'find_file_in_all_drives' is not defined"


Solution

  • You define find_file_in_all_drives for Window but you should define find_file_in_all_drives also for other systems - but every system will have different code in find_file_in_all_drives. And then you can use find_file_in_all_drives on every system

    # all systems use it so it should be defined for all
    
    def find_file(root_folder, rex):
        for root, dirs, files in os.walk(root_folder):
            for f in files:
                result = rex.search(f)
                if result:
                    file_path = os.path.join(root, f)
        return file_path  
    
    # define different `find_file_in_all_drives` for different systems     
    
    if platform.system() == 'windows':
    
        def find_file_in_all_drives(file_name):
            matching_files = list()
            # create a regular expression for the file
            rex = re.compile(file_name)
            for drive in win32api.GetLogicalDriveStrings().split('\000')[:-1]:
                file_path = find_file(drive, rex)
                if file_path:
                    matching_files.append(file_path)
            return matching_files
    
    if platform.system() in ('mac', 'linux'):
    
        def find_file_in_all_drives(file_name):
            return find_file("/", file_name)
    
    # now you can use `find_file_in_all_drives` on every system
    
    df_location = find_file_in_all_drives("AB_NYC_2019.csv")
    
    df = pd.read_csv(df_location[0], index_col=0)