python之工作目录和文件引用
1.参考
还没细看
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之工作目录和文件引用的更多相关文章
- GNU make简要介绍①指定变量、自动推导规则、清除工作目录过程文件
Makefile简介 在执行make之前需要一个命名为Makefile的特殊文件来告诉make需要做些什么. 当使用 make 工具进行编译时,工程中以下几种文件在执行 make 时将会被编译 (重新 ...
- 如何查看与更改python的工作目录?
在编写<机器学习实战>第二章kNN代码时遇到问题,即在自己编写好模块后,使用ipython进行import时,出现以下错误: 可知若想找到该模块,需将工作目录改变到当前文件(模块py文件) ...
- python基础--管理目录与文件
1) 文件夹 os.listdir() #显示文件夹下所有文件 os.getcwd() #获取当前工作目录 os.chdir() #切换目录 os.mkdir() #建立目录 os.path.exis ...
- [Python]切换工作目录|python将目录切换为脚本所在目录
Python使用os.chdir命令切换python工作目录 代码示例: In []: import os In []: os.system("pwd") /home/wangju ...
- python生成器 获取 目录下文件
# os.walk()和os.list 都是得到所有文件的列表, 如果目录下文件特别多, 上亿了, 我们就需要生成器的方式获取 # 要求目录下面没有目录, 会递归到子目录下面找文件, (如果有子目录可 ...
- python找递归目录中文件,并移动到一个单独文件夹中,同时记录原始文件路径信息
运营那边有个需求. 下载了一批视频文件,由于当时下载的时候陆陆续续创建了很多文件夹,并且,每个文件夹下面还有子文件夹以及视频文件,子文件夹下面有视频文件或者文件夹 现在因为需要转码,转码软件只能对单个 ...
- 使用Python实现不同目录下文件的拷贝
目标:要实现将一台计算机的共享文件夹中的文件备份到另一台计算机,如果存在同名的文件只要文件的大小和最后修改时间一致,则不拷贝该文件 python版本:Python3.7.1 python脚本: fro ...
- Python 调用上级目录的文件
程序结构如下: – src |-- mod1.py |-- lib | |-- mod2.py |-- sub | |-- test.py 具体代码如下: 在test.py里调用mod1 mod2 i ...
- [Python] 目录和文件操作
在Linux系统下用Python写脚本,肯定不能避免各种与目录和文件夹有关的操作.为了以后方便查阅,简单地针对Python中与目录和文件夹有关的操作进行汇总. 需要实现导入的模块为: import o ...
随机推荐
- php无法连接mysql问题解决方法总结
http://www.163ns.com/zixun/post/5295.html 本文章总结了在php开发中可能会常常碰到的一些php连接不了mysql数据库的一些问题总结与解决方法分享,有 ...
- 《Windows核心编程》读书笔记 上
[C++]<Windows核心编程>读书笔记 这篇笔记是我在读<Windows核心编程>第5版时做的记录和总结(部分章节是第4版的书),没有摘抄原句,包含了很多我个人的思考和对 ...
- PostgreSQL安装和创建用户和创建数据库
一.安装 可以参考postgresql官网安装教程:https://www.postgresql.org/download/linux/redhat/ Centos 6 安装postgresql 10 ...
- (转!)Pyinstaller 打包发布经验总结
原文地址 https://blog.csdn.net/weixin_42052836/article/details/82315118 具体的实现图待本人实现后贴上 原 Pyinstaller 打包发 ...
- 前端 -----jQuery的事件绑定和解绑
11-jQuery的事件绑定和解绑 1.绑定事件 语法: bind(type,data,fn) 描述:为每一个匹配元素的特定事件(像click)绑定一个事件处理器函数. 参数解释: type (S ...
- Shiro配置URL过滤
常用过滤器: anon 不需要认证 authc 需要认证 user 验证通过或RememberMe登录的都可以 URL说明: /admin?=authc 表示 ...
- Python-数据类型 主键auto_increment
MySQL数据操作: DML========================================================在MySQL管理软件中,可以通过SQL语句中的DML语言来实 ...
- 注册InstallShield
安装InstallShield 下载installshield limitededition版本,这个版本是免费的 注册 安装打开后会给一个网址要求进行注册 其中,国籍是必填项但是下拉菜单中没有内容, ...
- Modbus库开发笔记之七:Modbus其他辅助功能开发
前面开发了各种应用,但是却一直没有提到一个问题,你就是对具体的数据进行读写操作.对于Modbus来说标准的数据有4种:线圈数据(地址:0000x).输入状态量数据(地址:1000x).保持寄存器数据( ...
- 【MySql】like用法
LIKE用法 SELECT * FROM TABLE WHERE col Like '%a';//检索以a结尾的内容 SELECT * FROM TABLE WHERE col Like '%a%'; ...