sample1.py
pd.read_csv("sample.csv")
error
[Errno 2] No such file or directory: 'sample.csv'
??? "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'
??? "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.
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 !!!!)
pd.read_csv("File path specification")
I knew this, but first of all, the concept of "Kant directory" became important.
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.
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.
I'm an inexperienced pythoner, so if you make a mistake or make a mistake, please let me know. Have a good analyze life!
"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