1.参考

如何获得Python脚本所在目录的位置

Python 相对导入与绝对导入

还没细看

2.不考虑exe打包

sys.path[0]                                   #顶层运行脚本的绝对目录

os.path.split(os.path.realpath(__file__))[0]  #目前所在脚本的绝对目录
os.path.dirname(os.path.realpath(__file__))

3.兼容 pyinstaller xxx,py -F 所生成的exe可执行程序

生成exe之后需要手动生成子文件夹和相应的txt等非py文件

在任一脚本中(含子目录)获取顶层运行的exe或py文件的绝对目录

import os, sys
if sys.argv[0][-3:] == 'exe':
(top_dir, _) = os.path.split(sys.argv[0])
if top_dir == '':
top_dir = os.getcwd() #os.path.abspath('.') 也行
else:
top_dir = sys.path[0]

4.其他

(1)连接目录

# with open(os.path.join(os.getcwd(), '/sub/sub.txt')) as f:  #fail
# with open(os.path.join(os.getcwd(), 'sub/sub.txt')) as f: #pass
with open(os.path.join(os.getcwd(), './sub/sub.txt')) as f: #pass
print f.read()

(2)根据需要临时修改sys.path

sys.path.append('G:/xxx')

5.测试py

G:\test\path

____file:path.py

____dir:sub

________file:__init__.py

________file:sub_path.py

path.py

#!usr/bin/env python
#coding:utf-8 import os, sys
from sub.sub_path import print_sub_path def print_path():
print 'in path.py'
print '{:<20}: {}'.format('os.getcwd()', os.getcwd()) #命令提示符显示目录
print '{:<20}: {}'.format('os.path.abspath(".")', os.path.abspath('.')) #命令提示符显示目录
print '{:<20}: {}'.format('sys.argv[0]', sys.argv[0]) #命令提示符显示目录>之后除去python的所有字符 print '{:<20}: {}'.format('sys.path[0]', sys.path[0]) #自动将顶层运行脚本所在路径 加入sys.path即寻找模块的搜索路径列表
print '{:<20}: {}'.format('realpath(__file__)', os.path.split(os.path.realpath(__file__))[0]) #目前所在脚本的绝对目录 if __name__ == '__main__':
print_path()
print_sub_path()
raw_input(':')

sub.py

#!usr/bin/env python
#coding:utf-8 import os, sys def print_sub_path():
print 'in sub/sub_path.py' print '{:<20}: {}'.format('os.getcwd()', os.getcwd())
print '{:<20}: {}'.format('os.path.abspath(".")', os.path.abspath('.'))
print '{:<20}: {}'.format('sys.argv[0]', sys.argv[0]) print '{:<20}: {}'.format('sys.path[0]', sys.path[0])
print '{:<20}: {}'.format('realpath(__file__)', os.path.split(os.path.realpath(__file__))[0]) #目前所在脚本的绝对目录 if __name__ == '__main__':
print_sub_path()

运行结果:

C:\Users\win7>python G:\test\path\path.py
in path.py
os.getcwd() : C:\Users\win7
os.path.abspath("."): C:\Users\win7
sys.argv[0] : G:\test\path\path.py
sys.path[0] : G:\test\path
realpath(__file__) : G:\test\path
in sub/sub_path.py
os.getcwd() : C:\Users\win7
os.path.abspath("."): C:\Users\win7
sys.argv[0] : G:\test\path\path.py
sys.path[0] : G:\test\path
realpath(__file__) : G:\test\path\sub
: C:\Users\win7>cd g:
G:\ C:\Users\win7>g: G:\>python test/path/path.py
in path.py
os.getcwd() : G:\
os.path.abspath("."): G:\
sys.argv[0] : test/path/path.py
sys.path[0] : G:\test\path
realpath(__file__) : G:\test\path
in sub/sub_path.py
os.getcwd() : G:\
os.path.abspath("."): G:\
sys.argv[0] : test/path/path.py
sys.path[0] : G:\test\path
realpath(__file__) : G:\test\path\sub
: G:\>cd test/path G:\test\path>python path.py
in path.py
os.getcwd() : G:\test\path
os.path.abspath("."): G:\test\path
sys.argv[0] : path.py
sys.path[0] : G:\test\path
realpath(__file__) : G:\test\path
in sub/sub_path.py
os.getcwd() : G:\test\path
os.path.abspath("."): G:\test\path
sys.argv[0] : path.py
sys.path[0] : G:\test\path
realpath(__file__) : G:\test\path\sub
: G:\test\path>python G:\test\path\path.py
in path.py
os.getcwd() : G:\test\path
os.path.abspath("."): G:\test\path
sys.argv[0] : G:\test\path\path.py
sys.path[0] : G:\test\path
realpath(__file__) : G:\test\path
in sub/sub_path.py
os.getcwd() : G:\test\path
os.path.abspath("."): G:\test\path
sys.argv[0] : G:\test\path\path.py
sys.path[0] : G:\test\path
realpath(__file__) : G:\test\path\sub
:

6.测试exe

运行结果:

#直接双击 exe
in path.py
os.getcwd() : G:\test\path
os.path.abspath("."): G:\test\path
sys.argv[0] : G:\test\path\path.exe
sys.path[0] : C:\Users\win7\AppData\Local\Temp\_M5247~1
realpath(__file__) : G:\test\path
in sub/sub_path.py
os.getcwd() : G:\test\path
os.path.abspath("."): G:\test\path
sys.argv[0] : G:\test\path\path.exe
sys.path[0] : C:\Users\win7\AppData\Local\Temp\_M5247~1
realpath(__file__) : C:\Users\win7\AppData\Local\Temp\_M5247~1\sub
: C:\Users\win7>G:\test\path\path.exe
in path.py
os.getcwd() : C:\Users\win7
os.path.abspath("."): C:\Users\win7
sys.argv[0] : G:\test\path\path.exe
sys.path[0] : C:\Users\win7\AppData\Local\Temp\_M83A1~1
realpath(__file__) : C:\Users\win7
in sub/sub_path.py
os.getcwd() : C:\Users\win7
os.path.abspath("."): C:\Users\win7
sys.argv[0] : G:\test\path\path.exe
sys.path[0] : C:\Users\win7\AppData\Local\Temp\_M83A1~1
realpath(__file__) : C:\Users\win7\AppData\Local\Temp\_M83A1~1\sub
: C:\Users\win7>cd g:
G:\ C:\Users\win7>g: G:\>cd test/path G:\test\path>path.exe
in path.py
os.getcwd() : G:\test\path
os.path.abspath("."): G:\test\path
sys.argv[0] : path.exe
sys.path[0] : C:\Users\win7\AppData\Local\Temp\_M9C69~1
realpath(__file__) : G:\test\path
in sub/sub_path.py
os.getcwd() : G:\test\path
os.path.abspath("."): G:\test\path
sys.argv[0] : path.exe
sys.path[0] : C:\Users\win7\AppData\Local\Temp\_M9C69~1
realpath(__file__) : C:\Users\win7\AppData\Local\Temp\_M9C69~1\sub
: G:\test\path>G:\test\path\path.exe
in path.py
os.getcwd() : G:\test\path
os.path.abspath("."): G:\test\path
sys.argv[0] : G:\test\path\path.exe
sys.path[0] : C:\Users\win7\AppData\Local\Temp\_M9C4E~1
realpath(__file__) : G:\test\path
in sub/sub_path.py
os.getcwd() : G:\test\path
os.path.abspath("."): G:\test\path
sys.argv[0] : G:\test\path\path.exe
sys.path[0] : C:\Users\win7\AppData\Local\Temp\_M9C4E~1
realpath(__file__) : C:\Users\win7\AppData\Local\Temp\_M9C4E~1\sub
:

  

最新文章

  1. python调用其他程序或脚本方法(转)
  2. 9.Python笔记之面向对象高级部分
  3. 随机获取Mysql数据表的一条或多条记录
  4. 类似于QQ游戏百万人同时在线的服务器架构实现
  5. bzoj3998 [TJOI2015]弦论(SAM)
  6. 博客搬家到 http://fresky.github.io/ - Visual Studio的插件Pdbproj可以把pdb转换成C++项目
  7. 小贝_mysql建表以及列属性
  8. WebService第一天
  9. 2.跑nodejs文件
  10. css3实现图片旋转效果
  11. c# 解密微信encryptedData字段
  12. C\S 架构 DNS服务器 交换机 路由器
  13. Kafka实战解惑
  14. mybatis输出sql语句
  15. ZOJ 1002:Fire Net(DFS+回溯)
  16. Android 通过联系人姓名查询联系人号码
  17. jQuery mobile 初始化页面的过程
  18. 挂载镜像SD卡的FAT32文件系统分区到Linux中
  19. Android :Activity、Adapter、List的初步学习
  20. 20155328 2016-2017-2 《Java程序设计》第四周学习总结

热门文章

  1. boost::tokenizer详解
  2. P1262 间谍网络 (tarjan缩点 水过去)
  3. 【转】关于Log4j
  4. ssdb主从及双主模型配置和简单管理
  5. Redis高级特性介绍及实例分析
  6. 【原创】大数据基础之Flume(2)Sink代码解析
  7. 基于官方mysql镜像构建自己的mysql镜像
  8. Python-计算机硬件基础
  9. hbase搭建
  10. Oracle imp exp 导入导出 执行脚本