Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing csv from a subdirectory in Python

Tags:

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?

like image 654
acpigeon Avatar asked Jun 28 '26 00:06

acpigeon


2 Answers

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.

like image 113
Gareth Latty Avatar answered Jul 01 '26 19:07

Gareth Latty


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.

like image 38
Mihnea Simian Avatar answered Jul 01 '26 20:07

Mihnea Simian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!