How to get the current directory in Python

巴扎黑
Release: 2017-08-07 11:46:32
Original
2543 people have browsed it

本文给大家讲解的是使用python获取当前所在目录的方法以及相关示例,非常的清晰简单,有需要的小伙伴可以参考下

sys.path

模块搜索路径的字符串列表。由环境变量PYTHONPATH初始化得到。

sys.path[0]是调用Python解释器的当前脚本所在的目录。

sys.argv

一个传给Python脚本的指令参数列表。

sys.argv[0]是脚本的名字(由系统决定是否是全名)

假设显示调用python指令,如 python demo.py ,会得到绝对路径;

若直接执行脚本,如 ./demo.py ,会得到相对路径。

os.getcwd()

获取当前工作路径。在这里是绝对路径。
https://docs.python.org/2/library/os.html#os.getcwd

__file__

获得模块所在的路径,可能得到相对路径。

如果显示执行Python,会得到绝对路径。

若按相对路径来直接执行脚本 ./pyws/path_demo.py ,会得到相对路径。

为了获取绝对路径,可调用 os.path.abspath()

os.path 中的一些方法

os.path.split(path)

将路径名称分成头和尾一对。尾部永远不会带有斜杠。如果输入的路径以斜杠结尾,那么得到的空的尾部。

如果输入路径没有斜杠,那么头部位为空。如果输入路径为空,那么得到的头和尾都是空。
https://docs.python.org/2/library/os.path.html#os.path.split

os.path.realpath(path)

返回特定文件名的绝对路径。

https://docs.python.org/2/library/os.path.html#os.path.realpath

代码示例

环境 Win7, Python2.7

以 /e/pyws/path_demo.py 为例


#!/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()
Copy after login

在 /d 中运行,输出为


$ 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:\
Copy after login

在e盘中用命令行直接执行脚本


$ ./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:\
Copy after login

The above is the detailed content of How to get the current directory in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template