Python如何获取当前所在目录


当前第2页 返回上一页

以 /e/pyws/path_demo.py 为例


1

2

3

4

5

6

7

8

9

10

11

12

13

#!/usr/bin/env python

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--aes加密与解密方法指导

Python操作二维数组画图

Python在函数中使用列表作为默认参数的介绍(代码示例)

Python的用途有哪些?

盘点由Python开发的网站和应用

Python实现rsa算法

介绍Python中slice参数过长的处理方法及实例

哪个不是Python合法标识符

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...