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 ...
随机推荐
- 阿里云语音合成(汉语英语)带UI界面的小程序(python)
一,项目说明 将汉文转汉语.英文转英语,同时又有逗号<###English###>,<,,,>和句号<...>标志符用于文件处理.其中英文包含在### 英文 ### ...
- 【原创】大叔经验分享(74)nginx对静态文件加速
通过location配置 location ~ \.html$ { add_header 'Cache-Control' 'no-cache'; } location ~ \.(js|css|gif| ...
- 【weixi】微信支付---微信公众号JSAPI支付
一.JSAPI支付 JSAPI支付是用户在微信中打开商户的H5页面,商户在H5页面通过调用微信支付提供的JSAPI接口调起微信支付模块完成支付.应用场景有: ◆ 用户在微信公众账号内进入商家公众号,打 ...
- JS基础_算数运算符
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- O032、Nova reboot 和 lock 操作
参考https://www.cnblogs.com/CloudMan6/p/5479408.html 前面通过日志详细分析了 nova 的 launch.shutoff .start 操作. ...
- 设计模式及UML图
UML图 1. 依赖关系
- 微信小程序下拉刷新
<config> { enablePullDownRefresh: true, navigationBarTitleText: "消息中心", } </confi ...
- js扩展Date对象的方法,格式化日期格式(prototype)
扩展:Date.prototype.format = function(format){ var o = { "M+" : this.getMonth()+1, ...
- JAVA语言程序设计课后习题----第一单元解析(仅供参考)
1 本题是水题,基本的输出语句 public class test { public static void main(String[] args) { // 相邻的两个 "" 要 ...
- Python之for循环与while循环
for语句格式for x in range(起始值,结束值,步幅) 执行语句输出0,100各个数字for i in range(0,101) print(i)输出0,100的偶数for i in ra ...