os.system() 工作但 subprocess.popen() 不... FileNotFoundError: [WinError 2] 和 OSError: [WinError 193] 错误

时间:2021-07-13 18:22:36

标签: python windows operating-system subprocess

当我尝试运行一个简单的子流程行时,我遇到了一个奇怪的错误。我经常在需要该程序运行的计算机上收到“OSError: [WinError 193] %1 不是有效的 Win32 应用程序”,即使相同的代码行在另一台计算机上运行以打开随机文本文件.当我使用 os.system([path]) 时它可以工作,但我需要代码继续而不是等待被关闭。 这是我得到的一个例子

import subprocess
import os

open = subprocess.Popen('C://Users//z004c50a//Desktop//Thisworksfolder//thisWorks3.txt')
#open = os.system('C://Users//z004c50a//Desktop//Thisworksfolder//thisWorks3.txt')

输出:

使用 os.system 工作但暂停代码,所以我想使用 subprocess 方法,但这给了我以下错误:

Traceback (most recent call last):
  File "C:\Users\z004c50a\Desktop\PythonApplication2\PythonApplication2\PythonApplication2.py", line 4, in <module>
    open = subprocess.Popen('C://Users//z004c50a//Desktop//Thisworksfolder//thisWorks3.txt')
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\subprocess.py", line 800, in __init__
    restore_signals, start_new_session)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\subprocess.py", line 1207, in _execute_child
    startupinfo)
OSError: [WinError 193] %1 is not a valid Win32 application
Press any key to continue . . .

我尝试了很多 stdout = subprocess.pipe, shell = True 等的变体

另一个可以在其他地方运行而不是在计算机上运行的程序,我需要它才能运行...

///////////////////////////////////////////// /////////////////////////////////////////////////< /p>

#Iterate through path objects, find all that end with .txt

#This is the intial loop that
for item in p.glob('**/*'):
   
    if item.suffix in (['.txt']):
        name = item.name
        path = Path.resolve(item).parent
        size = item.stat().st_size
        modified = datetime.fromtimestamp(item.stat().st_mtime)

        files.append(File(name, path, size, modified))
        pathName =  '"' + str(path) + '\\' + str(name) + '"'
        updateFile.write("\n" + pathName)
        print(pathName)
        open = subprocess.call(pathName) #open the file
        #open = os.system(pathName) #open the file
        sys.exit()

输出:

"C:\Users\z004c50a\Desktop\ThisWorksfolder\thisWorks3.txt"
Traceback (most recent call last):
  File "C:\Users\z004c50a\Desktop\PythonCadMigration\PythonCadMigration\PythonCadMigration.py", line 71, in <module>
    open = subprocess.call(pathName) #open the file
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\subprocess.py", line 339, in call
    with Popen(*popenargs, **kwargs) as p:
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\subprocess.py", line 800, in __init__
    restore_signals, start_new_session)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\subprocess.py", line 1207, in _execute_child
    startupinfo)
OSError: [WinError 193] %1 is not a valid Win32 application
Press any key to continue . . .

///////////////////////////////////////////// /////////////////////////////////////////

当我将 shell 设置为 true subprocess.Popen('C://Users//z004c50a//Desktop//Thisworksfolder//thisWorks3.txt', shell = True) 时,我收到一个错误 FileNotFoundError: [WinError 2] 这更奇怪,因为如果我在 cmd 中输入该路径,那么它会像平常一样打开所以我知道文件在那里。这是一个例子

///////////////////////////////////////////// ///////////////////////////////////////////

import subprocess
subprocess.run("dir", shell =True)

输出

Traceback (most recent call last):
  File "C:\Users\z004c50a\Desktop\PythonCadMigration\PythonCadMigration\PythonCadMigration.py", line 32, in <module>
    subprocess.run("dir", shell =True)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\subprocess.py", line 488, in run
    with Popen(*popenargs, **kwargs) as process:
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\subprocess.py", line 800, in __init__
    restore_signals, start_new_session)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\subprocess.py", line 1207, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
Press any key to continue . . .

///////////////////////////////////////////// /////////////////////////////////////////////

我已经检查了我的路径,不相信这是问题所在,但我非常愿意接受有关该问题的任何帮助。提前致谢

1 个答案:

答案 0 :(得分:0)

我只用过一次 subprocess.Popen,但它有效。

subprocess.Popen([sys.executable, 'wintest.py'])

看起来第一个接受的 arg 应该是第二个索引中包含文件路径的列表。

相关问题