When playing with sample code in python Almost 100% of the file is read Poor situation where "File Not Found Error" occurs.
I thought that the reason why I didn't grow forever was because I didn't record my reflection. Keep a record
Originally there would be security issues as well It's embarrassing to show my messy folder structure to people To prevent the same thing from happening in the future I will not wear even one pair of pants, so I will keep going.
file organization
C:\Users\watya\protos>
├─kagglebook-master
│ ├─ch01
│ │ └─ch01-01-titanic.py
│ ├─ ・ ・ ・
│ ├─input
│ │ └─ch01-titanic
│ │ ├─gender_submission.csv
│ │ ├─test.csv
│ │ └─train.csv
… …
Command executed
C:\Users\watya\protos> & C:/Users/watya/Anaconda3/python.exe c:/Users/watya/protos/kagglebook-master/ch01/ch01-01-titanic.py
Executed program (only at the beginning)
import numpy as np
import pandas as pd
# -----------------------------------
#Reading training data and test data
# -----------------------------------
#Reading training data and test data
train = pd.read_csv('../input/ch01-titanic/train.csv')
test = pd.read_csv('../input/ch01-titanic/test.csv')
Error that occurred
FileNotFoundError: [Errno 2] File b'../input/ch01-titanic/train.csv' does not exist: b'../input/ch01-titanic/train.csv'
You can see it by looking at [Command executed] Actually C:\Users\watya\protos Even though it is running in c:/Users/watya/protos/kagglebook-master/ch01/ Because you are "misunderstanding" what you are doing in FileNotFoundError Is happening.
Based on this "misunderstanding", I think it would be good if we could take a calm response so that we could specify an appropriate path.
Reference: https://note.nkmk.me/python-os-getcwd-chdir/
Get / check current directory: use os.getcwd ()
You should use it like this
Executable file
import numpy as np
import pandas as pd
import os
print("Current location:{}".format(os.getcwd()))← Insert here
# -----------------------------------
#Reading training data and test data
# -----------------------------------
#Reading training data and test data
train = pd.read_csv('../input/ch01-titanic/train.csv')
test = pd.read_csv('../input/ch01-titanic/test.csv')
Execution result
PS C:\Users\watya\protos> & C:/Users/watya/Anaconda3/python.exe c:/Users/watya/protos/kagglebook-master/ch01/ch01-01-titanic.py
Current location:C:\Users\watya\protos ←pd.read_csv()Find out where the file was referenced
Traceback (most recent call last):
・ ・ ・
FileNotFoundError: [Errno 2] File b'../input/ch01-titanic/train.csv' does not exist: b'../input/ch01-titanic/train.csv'
At worst, you can avoid panic with this. If you know your current location, you can see the direction of the solution because you can understand that "I see, it's not good because you are referring from a completely different place."
I think there are several ways to do it, so I wrote all the ones that came to my mind (I think there are more ...) ① Make the argument of read_csv () an absolute path instead of a relative path. (2) Move the runtime path to the executable file before executing. In other words C:\Users\watya\protos> not C:\Users\watya\protos/kagglebook-master/ch01/> Run on ③ Use os.chdir () to move the execution directory after execution and refer to it.
Anything is fine, but personally I didn't have any pride I want to avoid tampering with the chords like ① because I often pluck the chords I personally like to add it in ③.
If you don't have to mess with the chord, ② is the most beautiful because you don't touch the chord. If you have a lot of forgetfulness, you will often get lost by skipping here. I think that ③ is suitable for personality.
When I wrote it all over, I wrote what happened to the code of ①②.
Executable file (in case of ①)
import numpy as np
import pandas as pd
# -----------------------------------
#Reading training data and test data
# -----------------------------------
#Reading training data and test data
train = pd.read_csv('c:/Users/watya/protos/kagglebook-master/input/ch01-titanic/train.csv')← Rewrote here
test = pd.read_csv('c:/Users/watya/protos/kagglebook-master/input/ch01-titanic/test.csv')← Rewrote here
Executable file (in case of ③)
import numpy as np
import pandas as pd
import os
os.chdir('kagglebook-master/ch01/')← Insert here
# -----------------------------------
#Reading training data and test data
# -----------------------------------
#Reading training data and test data
train = pd.read_csv('../input/ch01-titanic/train.csv')
test = pd.read_csv('../input/ch01-titanic/test.csv')
This time it's a countermeasure when you get lost in file reference It's a bit off the topic, but os.getcwd () is just a blatant way to use it in a debug. Normally, I wonder how it is to write code that spits out such a FileNotFoundError ... Also write down how to catch the error with the assert statement.
Executable file
import numpy as np
import pandas as pd
import os
#csv file existence check
assert os.path.isfile('../input/ch01-titanic/train.csv'), 'train.no csv'
assert os.path.isfile('../input/ch01-titanic/test.csv'), 'test.no csv'
# -----------------------------------
#Reading training data and test data
# -----------------------------------
#Reading training data and test data
train = pd.read_csv('../input/ch01-titanic/train.csv')
test = pd.read_csv('../input/ch01-titanic/test.csv')
Execution result
PS C:\Users\watya\protos> & C:/Users/watya/Anaconda3/python.exe c:/Users/watya/protos/kagglebook-master/ch01/ch01-01-titanic.py
Traceback (most recent call last):
File "c:/Users/watya/protos/kagglebook-master/ch01/ch01-01-titanic.py", line 9, in <module>
assert os.path.isfile('../input/ch01-titanic/train.csv'), 'train.no csv'
AssertionError: train.no csv
PS C:\Users\watya\protos>
It feels very refreshing. However, with this alone, why is there no file after all? It's not a solution, but it doesn't spit out a ferocious FileNotFoundError for the time being.
I thought I was writing a low-level article ... I think it's better to accept that this is the current level.
Also, if I write only this, I remember myself, and even if I forget it again If you search for "File Not Found Error" on Qiita, your article will be caught. This is already safe!
~ End ~
Recommended Posts