Issue
I'm trying to write a script that imports a file, then does something with the file and outputs the result into another file.
df = pd.read_csv('somefile2018.csv')
The above code works perfectly fine. However, I'd like to avoid hardcoding the file name in the code.
The script will be run in a folder (directory) that contains the script.py
and several csv files.
I've tried the following:
somefile_path = glob.glob('somefile*.csv')
df = pd.read_csv(somefile_path)
But I get the following error:
ValueError: Invalid file path or buffer object type: <class 'list'>
Solution
glob
returns a list, not a string. The read_csv
function takes a string as the input to find the file. Try this:
for f in glob('somefile*.csv'):
df = pd.read_csv(f)
...
# the rest of your script
Answered By - James
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.