python获取文件路径

时间:2023-03-10 06:04:08
python获取文件路径
摘自:https://blog.****.net/Poo_Chai/article/details/89764001

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(), "../..")))
————————————————
版权声明:本文为****博主「Poo_Chai」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.****.net/Poo_Chai/article/details/89764001

  python获取文件路径的代码如上所示。运行结果如下:

python获取文件路径