Skip to content
Advertisement

Python/pandas/os: get the files in this folder and iterate over those that fit this naming convention

I want to perform some data cleaning on all the files in the same folder as my script that fit a naming convention.

The data cleaning I am fine with, but it’s just the same folder that I am struggling with.

Previous working code:

import glob 
for filename in glob.glob('C:/Users/<me>/Downloads/New Folder/Forecast and Inventory *.xlsx'):

Current code:

import os
for filename in os.getcwd() + 'Forecast and Inventory *.xlsx':

I get the error code

No such file or directory: 'C'

Do I need to do some kind of replacement to turn the s into /s? As the code suggests, I want all files that start with ‘Forecast and Inventory’.

Thank you!

Advertisement

Answer

You are iterating through a string.

Since os.getcwd() + 'Forecast and Inventory *.xlsx' is the string "C://**working-dir/Forecast and Inventory *.xlsx"

So on the first iteration filename is the string 'C'.

For example

for filename in os.getcwd() + 'Forecast and Inventory *.xlsx':
    print(filename)

WIll return

"C"
":"
"/"
...

You just need to put the path inside glob.glob

for filename in glob.glob(os.getcwd() + 'Forecast and Inventory *.xlsx'):
    # do something
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement