acpigeon
acpigeon

Reputation: 1729

Importing csv from a subdirectory in Python

My project has a bunch of csv files that may or may not be called based on user input. I'd like to store these files in a subdirectory to keep my project folder uncluttered. I'm utterly confused as to how to do this. Most of the resources I've dug up involve importing a module or package from a subdirectory rather than a basic data file.

The line of code that imports my csv currently looks like:

target_doc = csv.reader(open('sample.csv', 'rU'), delimiter=",", quotechar='|')

I'm assuming a solution will involve setting up a path variable, using import os and import sys, and perhaps splitting this line multiple parts?

Upvotes: 2

Views: 54570

Answers (4)

Cagdas Kanar
Cagdas Kanar

Reputation: 763

Why don't you simply use :

path_in = '/directory_whatever_it_is/sample_data.csv'

sample_data = pd.read_csv(path_in)

Upvotes: 0

Gareth Latty
Gareth Latty

Reputation: 88977

You can open files by file path. Just use open('/path/to/file').

Importing is only necessary for modules and packages - Python source code.

The only real notes here are to use os.path.join() where joining paths for good compatibility across different operating systems and filesystems. The rest of the os.path module is also worth a look wherever files are involved. Another common trap with windows paths is that using backslashes escape characters, so you must escape your backslashes ("some\\file") - an ugly option, use raw strings (r"some\file"), use forward slashes (Python will actually handle this automatically), or - the best option, pass your path as arguments to the aforementioned os.path.join().

It may also be worth noting that using the with statement would improve your code.

e.g:

with open(os.path.join("path", "to", "file.csv"), 'rU') as file:
    target_doc = csv.reader(file, delimiter=",", quotechar='|')
    ...

Using with ensures your file gets closed - even if exceptions occur.

Upvotes: 8

Larry Lustig
Larry Lustig

Reputation: 50970

You are on the wrong path. Python path and import are only for "linking" other python code into your system. Other files used as resources by your system are accessed by specifying the full path of the file:

 target_doc = csv.reader(open('/resources/csv_files/sample.csv', 'rU'), delimiter=",", quotechar='|')

or a relative path from where you're running the python file:

 target_doc = csv.reader(open('csv_files/sample.csv', 'rU'), delimiter=",", quotechar='|')

Upvotes: 1

Mihnea Simian
Mihnea Simian

Reputation: 1103

You can use

import os
cwd = os.path.dirname(__file__) # get current location of script

And then use os.path.join(cwd, 'some/../path') to construct absolute paths to any location you like; so your paths are only relative to your script location, regardless of the current working directory for python interpretor. Mai also use os.sep if you don't want to depend on unix's '/'-convention.

Upvotes: 1

Related Questions