An existing file not being identified using os.path.isfile function

Question:

I have a file 7.csv in directory: '~/Documents/Jane/analyst/test/1/'. I was able to read this file using pandas.read_csv function with no problem.

f_path = '~/Documents/Jane/analyst/test/1/7.csv'

pd.read_csv(f_path, index_col=None, header=0)

But when checking whether this file is exsiting using os.path.isfile(), the result return False.

os.path.isfile(f_path)

False

What could be the the possible error source?

Asked By: enaJ

||

Answers:

Both os.path.isfile() and os.path.exists() do not recognize ~ as the home directory. ~ is a shell variable not recognized in python. It has to be either fully specified or you can use relative directory name.

But if you want to use ~ as home, you can do

from os.path import expanduser
home = expanduser("~")
Answered By: Hun

As hun mentioned, your code should be

import os

f_path = '~/Documents/Jane/analyst/test/1/7.csv'
os.path.isfile(os.path.expanduser(f_path))

This will expand the tilde into an absolute path. ~, . and .. do not have the same meaning to the python os package that they do in a unix shell and need to be interpreted by separate functions.

Answered By: sakurashinken

I came across this error with os.path.isfile not detecting a path which was captured from running a shell command with subprocess (which added a n to the end of the path string)

I detected this error by running shutil.move(file_path, new_path) without first checking if the source file existed with os.path.isfile

FileNotFoundError: [Errno 2] No such file or directory: '/home/stuart/devops/distrobuilder/build/alpinelinux-3.16-x86_64-default-20220814_1848.tar.xzn'

This sort of error is not apparent at first glance unless you are specifically looking for an extra new line when you print(file_path)

The solution in cases like this is to use strip() – in my case specifically on the command:

output = subprocess.check_output(cmd, shell=True, text=True).strip()
Answered By: Stuart Cardall
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.