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单词怎么读

Python中一些包的基本用处和安装方法

Python中的super()和__init__()方法有什么用

Python中文件的读取和写入操作

Python语言的特点是什么?

Python 怎么重命名文件

Python操作表格添加行

如何用Python编写乘法口诀表

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




打赏

取消

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

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

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

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

评论

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