1

My python code is in directory 'MAIN'. Inside MAIN, I have a folder 'DATA'.

Inside DATA, I have a CSV file and another folder '_OLD'

I want to import the CSV in DATA folder into a pandas dataframe, but I'm unsure of it's name.

How can I do that?

NOTE: There will always be exactly one CSV and the _OLD folder in the DATA folder. Thus I need a way to access the only file in that folder.

2
  • What have you tried so far? Commented Jun 21, 2019 at 10:11
  • I had no idea how to go about this problem, still a beginner. Got the answer though. Thanks! Commented Jun 21, 2019 at 10:22

3 Answers 3

6

You could do:

from glob import glob
filename = glob("MAIN/DATA/*.csv")[0]

EDIT: Another way is to use the pathlib library. One difference is that the glob() method of the Path object returns a generator of files in the given directory. The objects that are emitted are Path objects (that can be converted to string if needed using the str string constructor.

from pathlib import Path

for filename in Path("MAIN/DATA").glob("*.csv"):
    # do something
Sign up to request clarification or add additional context in comments.

Comments

0

try this,

files = os.listdir('DATA')
filtered_files = [file_  for file_ in files if file_.endswith('.csv')]
df1 = pd.read_csv('DATA'+'/'+filtered_files[0])

1 Comment

Thank you. This works perfectly, also let's me access the name of the file for later use.
0

You can use a wildcard to import it.

According to this answer, you can use the following code:

for f in glob('somefile*.csv'):
    df = pd.read_csv(f)
    ...

This will not result in an error

1 Comment

it will definitely throw FileNotFoundError

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.