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: estpath

____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:Userswin7>python G:	estpathpath.py
in path.py
os.getcwd()         : C:Userswin7
os.path.abspath("."): C:Userswin7
sys.argv[0]         : G:	estpathpath.py
sys.path[0]         : G:	estpath
realpath(__file__)  : G:	estpath
in sub/sub_path.py
os.getcwd()         : C:Userswin7
os.path.abspath("."): C:Userswin7
sys.argv[0]         : G:	estpathpath.py
sys.path[0]         : G:	estpath
realpath(__file__)  : G:	estpathsub
:

C:Userswin7>cd g:
G:

C:Userswin7>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:	estpath
realpath(__file__)  : G:	estpath
in sub/sub_path.py
os.getcwd()         : G:
os.path.abspath("."): G:
sys.argv[0]         : test/path/path.py
sys.path[0]         : G:	estpath
realpath(__file__)  : G:	estpathsub
:

G:>cd test/path

G:	estpath>python path.py
in path.py
os.getcwd()         : G:	estpath
os.path.abspath("."): G:	estpath
sys.argv[0]         : path.py
sys.path[0]         : G:	estpath
realpath(__file__)  : G:	estpath
in sub/sub_path.py
os.getcwd()         : G:	estpath
os.path.abspath("."): G:	estpath
sys.argv[0]         : path.py
sys.path[0]         : G:	estpath
realpath(__file__)  : G:	estpathsub
:

G:	estpath>python G:	estpathpath.py
in path.py
os.getcwd()         : G:	estpath
os.path.abspath("."): G:	estpath
sys.argv[0]         : G:	estpathpath.py
sys.path[0]         : G:	estpath
realpath(__file__)  : G:	estpath
in sub/sub_path.py
os.getcwd()         : G:	estpath
os.path.abspath("."): G:	estpath
sys.argv[0]         : G:	estpathpath.py
sys.path[0]         : G:	estpath
realpath(__file__)  : G:	estpathsub
:
View Code

6.测试exe

运行结果:

#直接双击 exe
in path.py
os.getcwd()         : G:	estpath
os.path.abspath("."): G:	estpath
sys.argv[0]         : G:	estpathpath.exe
sys.path[0]         : C:Userswin7AppDataLocalTemp\_M5247~1
realpath(__file__)  : G:	estpath
in sub/sub_path.py
os.getcwd()         : G:	estpath
os.path.abspath("."): G:	estpath
sys.argv[0]         : G:	estpathpath.exe
sys.path[0]         : C:Userswin7AppDataLocalTemp\_M5247~1
realpath(__file__)  : C:Userswin7AppDataLocalTemp\_M5247~1sub
:

C:Userswin7>G:	estpathpath.exe
in path.py
os.getcwd()         : C:Userswin7
os.path.abspath("."): C:Userswin7
sys.argv[0]         : G:	estpathpath.exe
sys.path[0]         : C:Userswin7AppDataLocalTemp\_M83A1~1
realpath(__file__)  : C:Userswin7
in sub/sub_path.py
os.getcwd()         : C:Userswin7
os.path.abspath("."): C:Userswin7
sys.argv[0]         : G:	estpathpath.exe
sys.path[0]         : C:Userswin7AppDataLocalTemp\_M83A1~1
realpath(__file__)  : C:Userswin7AppDataLocalTemp\_M83A1~1sub
:

C:Userswin7>cd g:
G:

C:Userswin7>g:

G:>cd test/path

G:	estpath>path.exe
in path.py
os.getcwd()         : G:	estpath
os.path.abspath("."): G:	estpath
sys.argv[0]         : path.exe
sys.path[0]         : C:Userswin7AppDataLocalTemp\_M9C69~1
realpath(__file__)  : G:	estpath
in sub/sub_path.py
os.getcwd()         : G:	estpath
os.path.abspath("."): G:	estpath
sys.argv[0]         : path.exe
sys.path[0]         : C:Userswin7AppDataLocalTemp\_M9C69~1
realpath(__file__)  : C:Userswin7AppDataLocalTemp\_M9C69~1sub
:

G:	estpath>G:	estpathpath.exe
in path.py
os.getcwd()         : G:	estpath
os.path.abspath("."): G:	estpath
sys.argv[0]         : G:	estpathpath.exe
sys.path[0]         : C:Userswin7AppDataLocalTemp\_M9C4E~1
realpath(__file__)  : G:	estpath
in sub/sub_path.py
os.getcwd()         : G:	estpath
os.path.abspath("."): G:	estpath
sys.argv[0]         : G:	estpathpath.exe
sys.path[0]         : C:Userswin7AppDataLocalTemp\_M9C4E~1
realpath(__file__)  : C:Userswin7AppDataLocalTemp\_M9C4E~1sub
:
View Code

  

原文地址:https://www.cnblogs.com/my8100/p/7353841.html