使用Python获取当前工作目录和执行命令的位置
获取当前工作目录
import sys
print(sys.path[0])
获取执行命令的位置
import os
print(os.getcwd())
补充知识:Python获取当前执行文件,根据某一级目录名称,获取此目录名称所在的绝对路径
假如当前文件绝对路径:E:learnpython我的filemy.py
#coding:utf-8import os #dirName:上级目录名称#sysCoding:系统编码格式#targetCoding:转换目标编码格式def get_dir_realpath(dirName,sysCoding,targetCoding): path = os.path.split(os.path.realpath(__file__))[0].decode(sysCoding).encode(targetCoding) dirList = path.split('') length = len(dirList) for _ in range(1,length): fileName = os.path.split(path)[1] path = os.path.split(path)[0] if fileName == dirName: return path break return '' print get_dir_realpath('我的file',’cp936’,'utf-8') 执行结果:E:learnpython
print get_dir_realpath('python',’cp936’,'utf-8') 执行结果:E:learn
以上这篇使用Python获取当前工作目录和执行命令的位置就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持好吧啦网。
相关文章:
1. ASP基础入门第二篇(ASP基础知识)2. python中24小时制转换为12小时制的方法3. Ajax 的初步实现(使用vscode+node.js+express框架)4. Python xlwings插入Excel图片的实现方法5. 一个用xslt样式将xml解析为xhtml的类TransformBinder(兼容FF和IE7.0)6. Python执行外部命令subprocess的使用详解7. 深入理解z-index的工作原理和应用技巧8. python 爬取华为应用市场评论9. 使用display:none时隐藏DOM元素无法获取实际宽高的解决方法10. 使用 Python 实现微信消息的一键已读的思路代码

网公网安备