Search code examples
pythonexcelpandasio

Searching for an excel file in two Directories and creating a path


I've recently posted a similar question a week about searching through sub directories to find a specific excel file. However this time, I need to find a specific file in either one of the two directories and give a path based on whether the file is located in one folder or the other.

Here is the code I have so far. the work computer i have is running on Python 2.7.18 - there are no errors however when i print out the df as an excel file nothing is shown in the output path

ExcelFilePath = sys.argv[1]
OutputFilePath = sys.argv[2]
    # path of excel directory and use glob glob to get all the DigSym files 
    for (root, subdirs, files) in os.walk(ExcelFilePath):
        for f in files:
            if '/**/Score_Green_*' in f and '.xlsx' in f:
                ScoreGreen_Files =  os.path.join(root, f)

                for f in ScoreGreen_Files:

                    df1 = pd.read_excel(f)
                    df1.to_excel(OutputFilePath)

Solution

  • OutputFilePath is an argument you're passing in. It isn't going to have a value unless you pass one in as a command line argument.

    If you want to return the path, the variable you need to return is ScoreGreen_Files. You also don't need to iterate through ScoreGreen_Files as it should just be the file you're looking for.

    ExcelFilePath = sys.argv[1]
    OutputFilePath = sys.argv[2]
    # path of excel directory and use glob glob to get all the DigSym files 
    for (root, subdirs, files) in os.walk(ExcelFilePath):
        for f in files:
            if '/**/Score_Green_*' in f and '.xlsx' in f:  # f is a single file
                ScoreGreen_File = os.path.join(root, f)
                df1 = pd.read_excel(ScoreGreen_File)
                df1.to_excel(OutputFilePath)