![](/img/trans.png)
[英]Python code that opens a file in the same directory works while running but not when debugging in VSCode
[英]Python subprocess Error 22 - Works while debugging but not when running
我正在尝试使用 subprocess.Popen subprocess.Popen()
从另一个调用 python 脚本。 当我逐行调试程序时它工作得很好,但是当我正常运行程序时,我收到以下错误:
C:\Python38\python.exe: can't open file '"<filepath>\thumbs.py"': [Errno 22] Invalid argument
我对问题是什么感到困惑,因为当程序被逐行调试时它可以正常工作,所以不确定程序正常运行时究竟发生了什么变化。
我正在尝试将一组 arguments 传递给子进程,然后在子进程中使用argparse
解析这些。
这就是我调用该过程的方式:
cmd = ['python', "\"" + pythonPath + "thumbs.py\"", '-d', "\"" + outputdb + "\"", '-c', "\"" + path + cache + "\""]
subprocess.Popen(cmd).wait()
这是我在子进程中解析 arguments 的方式:
if __name__ == '__main__':
global session
args = None
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--database", action="store", dest="dbname", help="Database to output results to", required=True)
parser.add_argument("-c", "--cache", action="store", dest="cachefile", help="Cache file to scrape.", required=True)
while args is None:
args = parser.parse_args()
谁能发现我错过了什么?
我设法通过首先创建我的命令和 arguments 作为字符串然后使用shlex.Split()
将其拆分为 arguments 列表来解决此问题。
cmdStr = "python \"" + pythonPath + "thumbs.py\" -d \"" + outputdb + "\" -c \"" + path + cache + "\""
cmd = shlex.split(cmdStr)
subprocess.Popen(cmd).wait()
更多信息在这里: https://docs.python.org/3.4/library/subprocess.html#popen-constructor
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.