python获取当前路径

import os,sys

使用sys.path[0]、sys.argv[0]、os.getcwd()、os.path.abspath(__file__)、os.path.realpath(__file__)

sys.path是Python会去寻找模块的搜索路径列表,sys.path[0]和sys.argv[0]是一回事因为Python会自动把sys.argv[0]加入sys.path。

如果你在C:\test目录下执行python getpath\getpath.py,那么os.getcwd()会输出“C:\test”,sys.path[0]会输出“C:\test\getpath”。

如果你用py2exe模块把Python脚本编译为可执行文件,那么sys.path[0]的输出还会变化:
如果把依赖库用默认的方式打包为zip文件,那么sys.path[0]会输出“C:\test\getpath\libarary.zip”;
如果在setup.py里面指定zipfile=None参数,依赖库就会被打包到exe文件里面,那么sys.path[0]会输出“C:\test\getpath\getpath.exe”。

#!/bin/env python
#-*- encoding=utf8 -*-

import os,sys

if __name__=="__main__":

    print "__file__=%s" % __file__

    print "os.path.realpath(__file__)=%s" % os.path.realpath(__file__)

    print "os.path.dirname(os.path.realpath(__file__))=%s" % os.path.dirname(os.path.realpath(__file__))
  
print "os.path.split(os.path.realpath(__file__))=%s" % os.path.split(os.path.realpath(__file__))[0]  
print "os.path.abspath(__file__)=%s" % os.path.abspath(__file__) print "os.getcwd()=%s" % os.getcwd() print "sys.path[0]=%s" % sys.path[0] print "sys.argv[0]=%s" % sys.argv[0]

输出结果:
D:\>python ./python_test/test_path.py
__file__=./python_test/test_path.py
os.path.realpath(__file__)=D:\python_test\test_path.py
os.path.dirname(os.path.realpath(__file__))=D:\python_test
os.path.split(os.path.realpath(__file__))=D:\python_test
os.path.abspath(__file__)=D:\python_test\test_path.py
os.getcwd()=D:\
sys.path[0]=D:\python_test
sys.argv[0]=./python_test/test_path.py

os.getcwd() “D:\”,取的是起始执行目录
sys.path[0]或sys.argv[0] “D:\python_test”,取的是被初始执行的脚本的所在目录
os.path.split(os.path.realpath(__file__))[0] “D:\python_test”,取的是__file__所在文件test_path.py的所在目录

 

正确获取当前的路径:

    __file__是当前执行的文件

 

    # 获取当前文件__file__的路径

    print "os.path.realpath(__file__)=%s" % os.path.realpath(__file__)

    # 获取当前文件__file__的所在目录

    print "os.path.dirname(os.path.realpath(__file__))=%s" % os.path.dirname(os.path.realpath(__file__))   
    # 获取当前文件__file__的所在目录

    print "os.path.split(os.path.realpath(__file__))=%s" % os.path.split(os.path.realpath(__file__))[0]  

 

转载于:https://www.cnblogs.com/wind-wang/p/5822192.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 Python 中,可以使用 `os` 模块来获取当前路径。具体方法如下: ```python import os current_path = os.getcwd() print(current_path) ``` 以上代码中,`os.getcwd()` 方法会返回当前 Python 脚本所在的目录的绝对路径,并将其赋值给变量 `current_path`。最后,使用 `print()` 函数输出 `current_path` 的值即可。 ### 回答2: 在Python中,我们可以使用`os`模块来获取当前路径。具体步骤如下: 首先,我们需要导入`os`模块: ```python import os ``` 然后,使用`os`模块中的`getcwd()`函数来获取当前路径,该函数返回一个字符串,表示当前的工作路径。代码如下: ```python current_path = os.getcwd() ``` 最后,我们可以使用`print()`函数来输出当前路径: ```python print("当前路径为:", current_path) ``` 完整代码如下: ```python import os current_path = os.getcwd() print("当前路径为:", current_path) ``` 这样,运行程序后,就可以在控制台上看到当前路径的输出信息了。 ### 回答3: 要获取当前路径,可以使用`os`模块中的`getcwd()`函数。具体步骤如下: 1. 首先,需要导入`os`模块,可以使用`import os`语句。 2. 然后,使用`getcwd()`函数获取当前路径,可以用`current_path = os.getcwd()`来实现。 3. 最后,可以打印出当前路径,使用`print(current_path)`即可。 以下是完整的代码示例: ```python import os current_path = os.getcwd() print(current_path) ``` 当你运行这段代码时,它将输出当前路径,例如`C:\Users\Username\Documents`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值