[PYTHON] [Note] Read a file from another directory

You do not have this experience? ?? ?? ?? ??

??? "Okay, I'll analyze it!"

sample1.py


pd.read_csv("sample.csv")

error


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

??? "Oh? Can't read it?"

??? "Yes, it's a file name, so it's no good. Okay, I'll copy the path and read it."

sample2.py


pd.read_csv("/clientA/user_data/payment/sample.csv")

error


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

??? "Oh? What's going on? What should I do rather than say?"

??? "It can't be helped. Do you want to store sample.csv in the working directory ..."

Yes. So, do you have such an experience? It's okay when there aren't many file procedures yet, but it's very troublesome when the capacity of a large file or PC is small in the first place. I was a beginner pythoner who repeated such a thing, but I did something to avoid increasing the number of analysis files.

This time, the memorandum is also open to the public.

Premise

The premise of this time is that the file structure is like this.

client_list --- clientA --- user_data --- payment ---  sample.csv
             |           |- analyze --- case1 -- analyze.ipynb
             |           |- quest_data ---・ ・ ・
             |           :
             |           :
             |
             |- clientB
             |- clientC
             |- clientD
             :
             :

This file directory will convey your environment in an easy-to-understand manner! I just gave it as an example, so my environment is not like this. (It's NG to say that the file structure is unnatural.)

What do you want to say at the end ** The file you want to reference is in another root of the directory above ** That is to say. (Yes, I don't know if the words are correct, but it's transmitted !!!!)

solution

pd.read_csv("File path specification")

I knew this, but first of all, the concept of "Kant directory" became important.

What is Kant Directory?

The Kant directory is simply ** "the directory (folder) you are currently working on" **. In the above skit, finally, by storing `` `sample.csv``` in the working directory, the solution was to store the working file in this "Kant directory" so that the analysis data can be handled. That's why it became.

How to browse files outside the Kant directory?

In order to refer to a file outside the Kant directory, it is important to go back to the directory where the file you want to refer to is located.

client_list --- clientA --- user_data --- payment ---  sample.csv
             |           |- analyze --- case1 -- analyze.ipynb
             |           |- quest_data ---・ ・ ・

In this example, you have to go back up two directories and go down `` `/ user_data / payment / sample.csv``` from there.

If you can write in your code to go back to the directory above this, you should be able to read it.

How to return the above directory


#When to go back one
"./" or "../"

#When to go back two
"././" or "../../"

And `./` or `` `../``` is added when specifying the file path, and it can be read. Please increase by the number of directories to return.

Therefore, in this example,

pd.read_csv("../../user_data/payment/sample.csv")

You can read it by writing.

Finally

I'm an inexperienced pythoner, so if you make a mistake or make a mistake, please let me know. Have a good analyze life!

Trivia

"Analysis folder", "Data storage folder" and "Output folder" If you separate these three, you can work very beautifully and comfortably.

output_dir = "./output"
input_dir =  "./input/"

df.read_csv(input_dir+"sample.csv")


sample2.to_csv(output_dir + "sample2.csv")

Recommended Posts

[Note] Read a file from another directory
Read line by line from a file with Python
How to read a file in a different directory
Read and write a file
Write and read a file
[Note] Import of a file in the parent directory in Python
Python> Read from a multi-line string instead of a file> io.StringIO ()
Save the Pydrive authentication file in a different directory from the script
Read a file in Python with a relative path from the program
Read file
Script to generate directory from json file
[GO language] Let's read a YAML file
Create a deb file from a python package
Read a character data file with numpy
How to read a CSV file with Python 2/3
Read a file containing garbled lines in Python
Python --Read data from a numeric data file and find the multiple regression line.
Draw a graph with matplotlib from a csv file
Run a Python file from html using Django
Mount a directory on another server with sshfs
Just a note
Python2 / numpy> Replace only a specific column in a file with column data from another file> numpy.c_
Make a copy of a Google Drive file from Python
Python-Read data from a numeric data file and calculate covariance
I tried running python etc. from a bat file
Backtrader How to import an indicator from another file
DJango Note: From the beginning (using a generic view)
Read QR code from image file with Python (Mac)
DJango Note: From the beginning (creating a view from a template)
Read logging settings from an external file in Flask
I tried reading data from a file using Node.js.
Python script to create a JSON file from a CSV file
[Python] Start a batch file from Python and pass variables.
[Python] File / directory operations
[Note] File reading ~ Python ~
Read CSV file: pandas
File read overhead measurement
A note about __call__
A note about subprocess
A note about mprotect (2)
Read Python csv file
[Python] Read From Stdin
Make a gif animation from a serial number file with matplotlib
Access the file with a relative path from the execution script.
[Python] Read command line arguments from file name or stdin