import os
root_path = os.path.abspath(os.path.join(os.getcwd(), ".."))
print("""*********************** Path test:start..... ********************""")
print("***获得当前路径***")
cwd = os.getcwd()
print('current path:', cwd)
print('File exist or not:', os.path.exists('C:\\Users\\leo\\Desktop\\neu.softwarecollege.mySocialCloudSimulator/' \
'//data/relationship-pairs.csv'))
print('File exist or not:', os.path.exists('../data/relationship-pairs.csv'))
print("""*********************** Path test:end..... ********************""")
print(os.path.abspath(os.path.dirname(__file__)))
print('***获取上级目录***')
print(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
print(os.path.abspath(os.path.dirname(os.getcwd())))
path = os.path.abspath(os.path.join(os.getcwd(), ".."))
print('path=', path)
print('File exist or not:', os.path.exists(path+'/data/relationship-pairs.csv'))
print('***获取上上级目录***')
print(os.path.abspath(os.path.join(os.getcwd(), "../..")))
python获取文件路径的代码如上所示。运行结果如下: