Link to home
Start Free TrialLog in
Avatar of tech
tech

asked on

FileNotFoundError: [Errno 2] No such file or directory: 'ford.csv'

Below is the code I am using to read multiple csv files from a folder. Files are present there but not reading it throwing file not found error.
import os
cwd = os.path.abspath('dirs')
print(cwd) # this is ok
files = os.listdir(cwd) 
print(files) # this is ok
df = pd.DataFrame()
for file in files:
    if file.endswith('.csv'):
         print(file) # this is ok; prints the file
        df = pd.read_csv(file) # file is there but FileNotFoundError

Open in new window

This is the output
/Users/tech/Desktop/2021/dirs
['ford.csv', 'ceby.csv', 'Sand.csv'
ford.csv
FileNotFoundError

Open in new window

Any ideas?
Avatar of Kimputer
Kimputer

It's totally correct, Python is not wrong at all.
If you want to process the files, use:

os.path.join(cwd, file)

Open in new window


Think about it more clearly, tell someone to do something with the file:

ford.csv

You think it's possible?

What if you tell someone to do something with this file instead?

/Users/tech/Desktop/2021/ford.csv

If you know the answer, you'll know the solution.
Avatar of tech

ASKER

Thanks, I could read csv files but as string not as csv files. I have multiple csv files in the folder which actually I wanted to read them separately as csv files and then process them separately. These errors TypeError: string indices must be integers or AttributeError: 'str' object has no attribute 'shape' were expected because of it being a string but I am not sure about the work around for this.

df = pd.DataFrame()
for file in files:
    print(file) 
    if file.endswith('.csv'):
        df = os.path.join(cwd, file)
        print(df.shape) # AttributeError: 'str' object has no attribute 'shape'
        df['DateTime'] = df['Date'].astype(str)+' '+df['Time'].astype(str)    TypeError: string indices must be integers
        df['DateTime'] = pd.to_datetime(df['DateTime'])

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Kimputer
Kimputer

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial