簡體   English   中英

Python 子進程錯誤 22 - 在調試時有效,但在運行時無效

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM