【问题标题】:os.system() works but subprocess.popen() doesnt... FileNotFoundError: [WinError 2] and OSError: [WinError 193] errorsos.system() 有效,但 subprocess.popen() 不... FileNotFoundError: [WinError 2] 和 OSError: [WinError 193] 错误
【发布时间】:2021-09-22 19:33:03
【问题描述】:

当我尝试运行一个简单的子进程行时,我遇到了一个奇怪的错误。我经常在需要程序运行的计算机上收到“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等尝试了很多变化。

另一个在其他地方运行但不在计算机上运行的程序我需要它运行...

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

#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 . . .

/////////////////////////////////////// //////////////////////////////////////////////p>

我已经检查了我的路径,不认为这是问题所在,但我非常愿意就问题提供任何帮助。提前致谢

【问题讨论】:

    标签: python windows operating-system subprocess


    【解决方案1】:

    我只使用过一次subprocess.Popen,但它正在工作。

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

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

    【讨论】:

      猜你喜欢
      • 2020-02-19
      • 2018-10-16
      • 2014-10-28
      • 2020-07-28
      • 1970-01-01
      • 1970-01-01
      • 2019-12-21
      • 2020-01-19
      • 1970-01-01
      相关资源
      最近更新 更多