[PYTHON] What to do if you get lost in file reference with FileNotFoundError

Introduction

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

Poor situation this time

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'

Cause of the error

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.

Countermeasure (1): Where are you running now? Understand and regain calm

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."

Action (2): Specify an appropriate path so that the file can be referenced.

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')

Extra: Use assert statements

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.

in conclusion

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

What to do if you get lost in file reference with FileNotFoundError
What to do if you get "coverage unknown" in Coveralls
What to do if you get a minus zero in Python
What to do if you get a UnicodeDecodeError with pip install
What to do if you get angry in TensorFlow v2 without attribute'app'
What to do if you get a TypeError with numpy min, max
What to do if you can't install with pip in babun environment
What to do if you get Could not fetch URL 443 with pip
What to do if you get a "No versions found" error in pipenv
What to do if you get angry with swapon failed: Operation not permitted
What to do if you get an error when installing python with pyenv
What to do if you get "Python not configured." Using PyDev in Eclipse
What to do if you get angry with "Value Error: unknown local: UTF-8" in python manage.py syncdb
What to do if you can't sort files with subscripts
What to do if you can't log in as root
What to do if you get an OpenSSL error when installing Python 2 with pyenv
What to do if you get `No kernel for language python found` in Hydrogen
What to do if you get "(35,'SSL connect error')" in pycurl (one of them)
What to do if you get an error when importing matplotlib in Python (Mac)
What to do if you get an Import Error when importing matplotlib with Jupyter
What to do if you run python in IntelliJ and end with an error
What to do if you can't install pyaudio with pip #Python
What to do if you can't build your project with Maven
What to do if you can't use the trash in Lubuntu 18.04.
What to do if you get Swagger-codegen in python and Import Error: No module named
What to do if you get a Cannot retrieve metalink for repository error in yum
What to do if you get an error when running "certbot renew" in CakePHP environment
What to do if you get an Undefined error when trying to use pip with pyenv
What to do if you get a Call with too many input arguments error at DoAndReturn in a golang test
What to do if you can't find well with grep's -f option
What to do if you get angry with'vertices' must be a 2D list ... in matplotlib arrow
What to do if pipreqs results in UnicodeDecodeError
What to do if you can't find PDO in Laravel or CakePHP
What to do if you couldn't send an email to Yahoo with Python.
What to do if you can't use scikit grid search in Python
What to do if you get stuck during Anaconda installation on Linux
What to do if you get an error saying c compiler cannot create executables in configure
If you want to get multiple statistics with groupby in pandas v1
What to do if you get an error when trying to load mnist
What to do if you get a must override `get_config` error when trying to model.save in Keras
What to do to get google spreadsheet in python
What to do if you get an error when installing Dlib (Ubuntu)
If you get lost with HTTP redirects 301 and 302
EC2 / Amazon Linux2: What to do if you get an "unable to execute'gcc': No such file or directory" error with pip install
What to do if you get angry with "Gtk * backend requires pygtke to be installed" even though pygtk is included in matplotlib
What to do if you get a "Wrong Python Platform" warning when using Python with the NetBeans IDE
[Python] What to do if you get a ModuleNotFoundError when importing pandas using Jupyter Notebook in Anaconda
What to do if you can't pip install mysqlclient
No module named What to do if you get'libs.resources'
ModuleNotFoundError: No module What to do if you get'tensorflow.contrib'
Links to do what you want with Sublime Text
What to do if a UnicodeDecodeError occurs in pip
What to do if you get an "unknown service" error from your gRPC server
What to do if you get a memory error when converting from PySparkDataFrame to PandasDataFrame
What to do if Sort imports get stuck in VS Code's Python Extension (around 2020/09)
What to do if you get the error ʻERR_FEATURE_UNAVAILABLE_ON_PLATFORM` when using ts-node-dev on Linux
What to do if you can't hit the arrow keys in the Python interactive console
[AWS] What to do when you want to pip with Lambda
What to do if ʻarguments [0] .scrollIntoView ();` fails in python selenium
What to do if pip gives a DistributionError in Homebrew
What to do if a 0xC0000005 error occurs in tf.train.start_queue_runners ()