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
:

  

python之工作目录和文件引用的更多相关文章

  1. GNU make简要介绍①指定变量、自动推导规则、清除工作目录过程文件

    Makefile简介 在执行make之前需要一个命名为Makefile的特殊文件来告诉make需要做些什么. 当使用 make 工具进行编译时,工程中以下几种文件在执行 make 时将会被编译 (重新 ...

  2. 如何查看与更改python的工作目录?

    在编写<机器学习实战>第二章kNN代码时遇到问题,即在自己编写好模块后,使用ipython进行import时,出现以下错误: 可知若想找到该模块,需将工作目录改变到当前文件(模块py文件) ...

  3. python基础--管理目录与文件

    1) 文件夹 os.listdir() #显示文件夹下所有文件 os.getcwd() #获取当前工作目录 os.chdir() #切换目录 os.mkdir() #建立目录 os.path.exis ...

  4. [Python]切换工作目录|python将目录切换为脚本所在目录

    Python使用os.chdir命令切换python工作目录 代码示例: In []: import os In []: os.system("pwd") /home/wangju ...

  5. python生成器 获取 目录下文件

    # os.walk()和os.list 都是得到所有文件的列表, 如果目录下文件特别多, 上亿了, 我们就需要生成器的方式获取 # 要求目录下面没有目录, 会递归到子目录下面找文件, (如果有子目录可 ...

  6. python找递归目录中文件,并移动到一个单独文件夹中,同时记录原始文件路径信息

    运营那边有个需求. 下载了一批视频文件,由于当时下载的时候陆陆续续创建了很多文件夹,并且,每个文件夹下面还有子文件夹以及视频文件,子文件夹下面有视频文件或者文件夹 现在因为需要转码,转码软件只能对单个 ...

  7. 使用Python实现不同目录下文件的拷贝

    目标:要实现将一台计算机的共享文件夹中的文件备份到另一台计算机,如果存在同名的文件只要文件的大小和最后修改时间一致,则不拷贝该文件 python版本:Python3.7.1 python脚本: fro ...

  8. Python 调用上级目录的文件

    程序结构如下: – src |-- mod1.py |-- lib | |-- mod2.py |-- sub | |-- test.py 具体代码如下: 在test.py里调用mod1 mod2 i ...

  9. [Python] 目录和文件操作

    在Linux系统下用Python写脚本,肯定不能避免各种与目录和文件夹有关的操作.为了以后方便查阅,简单地针对Python中与目录和文件夹有关的操作进行汇总. 需要实现导入的模块为: import o ...

随机推荐

  1. 设计模式C++学习笔记之五(Factory Method工厂方法模式)

      工厂方法模式的意义是定义一个创建产品对象的工厂接口,将实际创建工作推迟到子类当中.核心工厂类不再负责产品的创建,这样核心类成为一个抽象工厂角色,仅负责具体工厂子类必须实现的接口,这样进一步抽象化的 ...

  2. 用Openssl计算ECDSA签名

    ECDSA的全名是Elliptic Curve DSA,即椭圆曲线DSA.它是Digital Signature Algorithm (DSA)应用了椭圆曲线加密算法的变种.椭圆曲线算法的原理很复杂, ...

  3. 2018谷歌I/O开发者大会8大看点汇总 新品有哪些

    2018谷歌I/O开发者大会8大看点汇总 新品有哪些美国科技媒体The Verge近日撰文,列举了在即将召开的2018年谷歌I/O开发者大会上的8大看点,包括Android P.人工智能等等. 以下为 ...

  4. list_test

    #! -*- coding:utf-8 -*-"""len() ,len(list)方法返回列表元素个数,list -- 要计算元素个数的列表,返回值,返回列表元素个数元 ...

  5. iOS 去除高德地图下方的 logo 图标

    [self.mapView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, ...

  6. swift 实践- 13 -- UIStepper

    import UIKit class ViewController: UIViewController { var stepper: UIStepper! var label: UILabel! ov ...

  7. Confluence 6 配置服务器基础地址备注

    使用不同 URL.如果你配置了不同的基础 URL 地址或者你站点的访问者使用了不同的 URL 地址来访问你的 Confluence 地址,你有很大概率可能会受到错误信息. 修改上下文地址.如果你修改了 ...

  8. Confluence 6 跟踪你安装中的自定义修改

    在 Confluence 中的系统信息(System Information)部分,有一个 修改(Modification)的选项.在这个选项中列出了自你 Confluence 安装以来,你 Conf ...

  9. web的分页方法

    web分页的三种方式,闲来无事总结一下. 1.使用前端表格插件进行分页 例如用bootstrap的拓展table组件,注意设置其分页属性时设置为"client", 即是 sideP ...

  10. Android 框架 Afinal使用

    介绍android Afinal框架功能: Afinal是一个开源的android的orm和ioc应用开发框架.在android应用开发中,通过Afinal的ioc框架,诸如UI绑定,事件绑定,通过注 ...