当前第2页 返回上一页
以 /e/pyws/path_demo.py 为例
1 2 3 4 5 6 7 8 9 10 11 12 13 | import os
import sys
if __name__ = = '__main__' :
print "sys.path[0] =" , sys.path[ 0 ]
print "sys.argv[0] =" , sys.argv[ 0 ]
print "__file__ =" , __file__
print "os.path.abspath(__file__) =" , os.path.abspath(__file__)
print "os.path.realpath(__file__) = " , os.path.realpath(__file__)
print "os.path.dirname(os.path.realpath(__file__)) =" , os.path.dirname(os.path.realpath(__file__))
print "os.path.split(os.path.realpath(__file__)) =" , os.path.split(os.path.realpath(__file__))
print "os.getcwd() =" , os.getcwd()
|
在 /d 中运行,输出为
1 2 3 4 5 6 7 8 9 | $ python / e / pyws / path_demo.py
sys.path[ 0 ] = E:\pyws
sys.argv[ 0 ] = E: / pyws / path_demo.py
__file__ = E: / pyws / path_demo.py
os.path.abspath(__file__) = E:\pyws\path_demo.py
os.path.realpath(__file__) = E:\pyws\path_demo.py
os.path.dirname(os.path.realpath(__file__)) = E:\pyws
os.path.split(os.path.realpath(__file__)) = ( 'E:\\pyws' , 'path_demo.py' )
os.getcwd() = D:\
|
在e盘中用命令行直接执行脚本
1 2 3 4 5 6 7 8 9 | $ . / pyws / path_demo.py
sys.path[ 0 ] = E:\pyws
sys.argv[ 0 ] = . / pyws / path_demo.py
__file__ = . / pyws / path_demo.py
os.path.abspath(__file__) = E:\pyws\path_demo.py
os.path.realpath(__file__) = E:\pyws\path_demo.py
os.path.dirname(os.path.realpath(__file__)) = E:\pyws
os.path.split(os.path.realpath(__file__)) = ( 'E:\\pyws' , 'path_demo.py' )
os.getcwd() = E:\
|
以上就是Python如何获取当前所在目录的详细内容,更多文章请关注木庄网络博客!!
返回前面的内容
相关阅读 >>
Python扩展内置类型的实现方法分析
【插入排序实现】Python
用 Python 连接 mysql 的几种方式详解_Python
Python实现按当前日期(年、月、日)创建多级目录的方法
继承中的mro与super详解
Python中单下划线和双下划线有什么区别
Python编程如何判别线性
Python在财务里面有用吗
Python扫描proxy并且如何获取可用代理ip的示例分享
Python递归求阶乘的方法
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » Python如何获取当前所在目录