1

The code that I have determines which Operating System is being used. Then it has to search the entire system for my csv file. When it's found I need to be able to read in the csv file (so that its not just inside the function, but useable throughout my code).

So far I am able to locate my file, but I am having trouble to assign the filepath to a variable, so that I can read in that variabel with pd.read_csv()

the code that I have is at follows:

import pandas as pd
import os
import re
import win32api

# https://stackoverflow.com/questions/13067686/search-files-in-all-drives-using-python
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:
                    print(os.path.join(root, f))
                    return result
                    break # if you want to find only one

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

#file_name = "AB_NYC_2019.csv"
#find_file_in_all_drives(file_name)

df_location = find_file_in_all_drives( "AB_NYC_2019.csv" )
df = pd.read_csv(df_location)

I think that something is not right with the return.

Thank you for your time.

Right now it returns "None"

1 Answer 1

1

You haven't returned anything from anywhere.

I'm considering your code to be working and I've placed the necessary return calls but haven't tested it:

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")
first_file_df = pd.read_csv(df_location[0]) 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much! I am entirely new to Python and composed this code based on many other threads. This however is the missing key for it to work as I need it to work! I wish I could up vote your solution, but unfortunately my account is not allowed to do that yet. I am very thankful tho!!!
@TaliesinRSalomonson please select the tick on the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.