python之工作目录和文件引用

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
:
View Code

 

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
:
View Code

 

 

 

  

posted @ 2017-08-13 17:44  my8100  阅读(573)  评论(0编辑  收藏  举报