python获取当前路径几种方式
python中操作路径,最好用绝对路径。这样会避免在多个脚本调用过程中或不同层级调用过程中出现混乱[*]os.getcwd()
[*]sys.argv or sys.path
[*]__file__
python os模块中的 os.path.realpath()函数接收一个模块,返回绝对路径
配合__file__一起使用:
os.path.realpath(__file__)
这样可以返回一个文件的绝对路径,且不会出现由于在别的目录中,调用该脚本而出现路径混乱的现象。
页:
[1]