python-获取程序的路径
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]
python-获取程序的路径的更多相关文章
- golang获取程序运行路径
golang获取程序运行路径: /* 获取程序运行路径 */ func getCurrentDirectory() string { dir, err := filepath.Abs(filepath ...
- C#、ASP.NET获取当前应用程序的绝对路径,获取程序工作路径 (转帖)
C#.ASP.NET获取当前应用程序的绝对路径,获取程序工作路径 ============================================ 使用 Application.Start ...
- delphi根据进程PID获取程序所在路径的函数(用OpenProcess取得句柄,用GetModuleFileNameEx取得程序名)
uses psapi; {根据进程PID获取程序所在路径的函数}function GetProcessExePath(PID: Cardinal): string;varpHandle: THandl ...
- Qt 程序获取程序所在路径、用户目录路径、临时文件夹等特殊路径的方法
Qt 程序获取程序所在路径.用户目录路径.临时文件夹等特殊路径的方法 经常我们的程序中需要访问一些特殊的路径,比如程序所在的路径.用户目录路径.临时文件夹等.在 Qt 中实现这几个功能所用的方法虽然都 ...
- python获取当前文件路径
python获取当前文件路径 学习了:https://www.cnblogs.com/strongYaYa/p/7200357.html https://blog.csdn.net/heatdeath ...
- Python获取程序运行目录和脚本目录
Python获取程序运行目录和脚本目录 import os import sys #获取脚本所在目录 print os.path.split( os.path.realpath( sys.argv[0 ...
- dotnet 获取程序所在路径的方法
在 dotnet 有很多方法可以获取当前程序所在的路径,但是这些方法获取到的路径有一点不相同,特别是在工作路径不是当前的程序所在的路径的时候 通过下面几个方法都可以拿到程序所在的文件夹或程序文件 Ap ...
- 【转】MFC获取程序目录路径方法
原文网址:http://yeahyuanqing.blog.163.com/blog/static/118025091201149480818/ MFC获得当前应用程序目录的GetCurrentDir ...
- [转]WPF 获取程序启动路径
本文转自:http://hi.baidu.com/lilipangtou/item/f1b7488e3c92c4d05e0ec1ab 在Windows Form程序中,获取自身的启动目录是非常容易的, ...
- C# WinForm获取程序所在路径方法
多个获取WinForm程序所在文件夹路径的方法,收藏备忘. 1)获取当前进程的完整路径,包含文件名(进程名). 代码:string str =this.GetType().Assembly.Locat ...
随机推荐
- java语言中使用三元式的时候应该注意的问题
今天在项目中改领导要求的代码表现的时候发现了一个很有趣的问题. 但是的代码情况类似如下: 1 2 Integer test1 = null; System.out.println("test ...
- 编译程序遇到问题 relocation R_X86_64_32 against `.rodata' can not be used when making a shared object;
编译程序遇到问题 relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; 发现编译 ...
- luogu P4428 [BJOI2018]二进制
luogu 先考虑怎样的二进制串才会被3整除.可以发现如果二进制位第\(0,2,4...2n\)位如果为\(1\),那么在模3意义下为1,如果二进制位第\(1,3,5...2n+1\)位如果为\(1\ ...
- LeetCode 腾讯精选50题--有效的括号
根据题意,第一反应就是使用栈,左右括号相匹配,则将左括号出栈,否则将左括号入栈. 这里我用数组配合“指针”模拟栈的入栈与出栈操作,初始时指针位置指向0,表示空栈,凡遇上左括号则直接入栈,若遇上有括号, ...
- HLSL中constant variables的packing规则
HLSL中constant variables的packing规则 参考MSDN上的官方文档.一般而言,HLSL将数据打包为4字节对齐,此外,它不允许数据跨16字节(即4个float的vector)的 ...
- Redis之过期策略
一.设置过期时间 Redis对存储值的过期处理实际上是针对该值的键(key)处理的,即时间的设置也是设置key的有效时间.Expires字典保存了所有键的过期时间,Expires也被称为过期字段. e ...
- 【Git的基本操作八】添加、提交、查看状态
添加.提交.查看状态操作 查看状态: git status
- 如何从git上批量拉取本地当前分支的所有代码
1 我本地有一个文件_netrc文件里面存储这git的相关信息,包括用户名和密码 你可以根据自己的git相关信息配置自己的信息 2 我当前的分支是dev,我要拉取当前分支下的所有代码,编写shell脚 ...
- winfrom 操作Excel
利用Aspose.Cells.dll 操作Excel,内容如下: 1.界面设计: 2.逻辑: using System; using System.Collections.Generic; using ...
- Delphi 动态链接库编程
樊伟胜