範例:
f_path = '~/Documents/test.csv' os.path.isfile(f_path) False
解法:
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 REALLY want to use ~
as home, you can do
from os.path import expanduser
home = expanduser("~")
也可以這樣子用:
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))