Python windows下subprocess模块 cwd 参数不支持相对路径

发布时间 2023-12-27 14:40:47作者: 悟透

前言全局说明

Python windows下subprocess模块 cwd 参数不支持相对路径


一、问题

程序要执行命令,用到了 subprocess 模块,并指定了 cwd 运行路径,在 MAC系统下运行正常,在Windows 下运行报错。

经过查询,是系统差异导致,所以为了方便,在windows 下获取当前路径后拼接再生成绝对路径


二、

current_path = os.path.abspath(os.path.dirname(__file__))
file_PATH=f"{current_path}\\t_jar"
signature = subprocess.check_output(cmd, shell=True, cwd=file_PATH)

注意:在 win python下,将复制过来的路径 一个\ 改成两个\ 。一个\是转义的意思。


三、

官方:https://docs.python.org/zh-cn/3.6/library/subprocess.html
原文:https://stackoverflow.com/questions/21412610/subprocess-popen-using-relative-paths
译文:https://www.coder.work/article/367533




免责声明:本号所涉及内容仅供安全研究与教学使用,如出现其他风险,后果自负。




参考、来源:
https://pythonjishu.com/dibafmoreavbeez/