1

[PYTHON HELP]

Hello, I would like some help figuring out how to import data from n number of files within a folder. When I try to extract the actual data I get an error stating that the file/directory does not exist.

import os, csv
path = ("my directory")
files = sorted(os.listdir(path)) 


def f():
    for file in (files):
         with open(file, 'r') as csvfile:
                    data = csvfile.read
                    print(data)

OUTPUT:


 File "<ipython-input-158-1f4c11da5a68>", line 1, in <module>
    f()
 File "<ipython-input-157-b977510dbfcd>", line 8, in f
    with open(file, 'r') as csvfile:

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

1 Answer 1

1

os.listdir returns just the file names, without their path names, so you would need to join the path names with the file names when you open them.

Change:

with open(file, 'r') as csvfile:

to:

with open(os.path.join(fcm_path, file), 'r') as csvfile:
Sign up to request clarification or add additional context in comments.

Comments

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.