001、对比os.path.abspath()、__file__、os.getcwd()的区别
2020-07-25 16:38
351 查看
文章目录
一、关于sys.path系统路径
python执行py文件的时候,默认就会把当前运行环境的相关目录增加到sys.path系统路径中。
1.在pycharm中直接运行脚本:
print(sys.path, '00001')# ['D:\\Test_it', 'D:\\Test_it', 'D:\\Program Files\\JetBrains\\PyCharm 2020.1\\plugins\\python\\helpers\\pycharm_display', 'C:\\Python38\\python38.zip', 'C:\\Python38\\DLLs', 'C:\\Python38\\lib', 'C:\\Python38', 'C:\\Python38\\lib\\site-packages', 'D:\\Program Files\\JetBrains\\PyCharm 2020.1\\plugins\\python\\helpers\\pycharm_matplotlib_backend'] 00001
2.在命令行界面执行脚本:
print(sys.path, '00001')# ['D:\\Test_it', 'C:\\python38\\python38.zip', 'C:\\python38\\DLLs', 'C:\\python38\\lib', 'C:\\python38', 'C:\\python38\\lib\\site-packages'] 00001
二、os.path.abspath()
获得当前脚本存放的绝对路径。
三、_ _ file _ _
获得当前脚本存放的绝对路径,在pycharm中与Windows命令行界面执行的结果是路径分割符不一致,pycharm给优化成了与win不一致的分隔符。在通过os.path.normpath()与os.path.abspath()优化后,得到的路径就与win系统路径规则一致。
四、os.getcwd()
得到当前脚本的工作目录(并非脚本存放的绝对路径)。
五、对比运行结果
1.在pycharm中直接运行脚本
print(os.path.abspath(__file__))# D:\Test_it\start.pyprint(__file__)# D:/Test_it/start.pyprint(os.path.normpath(__file__))# D:\Test_it\start.pyprint(os.getcwd())# D:\Test_itprint(os.path.normpath(os.getcwd()))# D:\Test_it
2.在windows命令行界面执行脚本
print(os.path.abspath(__file__))# D:\Test_it\start.pyprint(__file__)# D:\Test_it\start.pyprint(os.path.normpath(__file__))# D:\Test_it\start.pyprint(os.getcwd())# C:\Users\Bayer04print(os.path.normpath(os.getcwd()))# C:\Users\Bayer04
:\Users\Bayer04
print(os.path.normpath(os.getcwd()))
C:\Users\Bayer04
相关文章推荐
- Python中获取路径os.getcwd()和os.path.dirname(os.path.realpath(__file__))的区别和对比
- Python中获取路径os.getcwd()和os.path.dirname(os.path.realpath(__file__))的区别和对比
- Python中获取路径os.getcwd()和os.path.dirname(os.path.realpath(__file__))的区别和对比
- Python中获取路径os.getcwd()和os.path.dirname(os.path.realpath(__file__))的区别和对比
- Python中获取路径os.getcwd()和os.path.dirname(os.path.realpath(__file__))的区别和对比
- python的os.path.realpath(__file__),os.getcwd(),sys.path() 的区别
- os.getcwd()和os.path.realpath(__file__)的区别
- os.path.dirname(__file__)和abspath()的用法
- os.getcwd()与os.path.dirname(__file__)
- 认识下os.path.dirname(__file__)和os.path.abspath(__file__)
- python3 os.path.realpath(__file__) 和 os.path.cwd() 方法的区别
- Python os.path.abspath(__file__) 获取脚本完整路径
- os.path.dirname(os.path.dirname(__file__)) 与os.path.dirname(os.path.abspath(__file__))
- os.path.abspath(__file__)和os.path.dirname(path)
- 检查某目录或是文件是否存在 file_exists( ABSPATH . 'wp-config.php')
- File.delete()和Files.delete(Path path)的区别
- spring中ClassPathXmlApplicationContext 和FileSystemXmlApplicationContext的区别
- bafe Python 3.6:join() 方法与os.path.join()的区别
- android异常 More than one file was found with OS independent path 'META-INF/XXX'
- Python中的os.path.dirname(__file__)