首页 > 解决方案 > Python 找不到某些 Windows 可执行文件

问题描述

我似乎无法让一些特定的可执行文件在 Windows 上的 python 中运行。在运行这些可执行文件时,我得到一个找不到文件的错误。

我可以使用多种方法让其他可执行文件(如 winver.exe)运行:

subprocess.Popen( [r"C:\Windows\System32\winver.exe"] )
subprocess.run( [r"C:\Windows\System32\winver.exe"] )
subprocess.run( ["winver"] )
subprocess.Popen( ["winver"] )
os.system( "winver" )

但我似乎无法让 wsl、bash、ssh 或 sftp 工作:

subprocess.Popen( [r"C:\Windows\System32\wsl.exe"] )
subprocess.run( [r"C:\Windows\System32\wsl.exe"] )
subprocess.run( ["wsl"] )
subprocess.Popen( ["wsl"] )
os.system( "wsl" )

subprocess.Popen( [r"C:\Windows\System32\bash.exe"] )
subprocess.run( [r"C:\Windows\System32\bash.exe"] )
subprocess.run( ["bash"] )
subprocess.Popen( ["bash"] )
os.system( "bash" )

subprocess.Popen( [r"C:\Windows\System32\OpenSSH\ssh.exe"] )
subprocess.run( [r"C:\Windows\System32\OpenSSH\ssh.exe"] )
subprocess.run( ["ssh"] )
subprocess.Popen( ["ssh"] )
os.system( "ssh" )

subprocess.Popen( [r"C:\Windows\System32\OpenSSH\sftp.exe"] )
subprocess.run( [r"C:\Windows\System32\OpenSSH\sftp.exe"] )
subprocess.run( ["sftp"] )
subprocess.Popen( ["sftp"] )
os.system( "sftp" )

sftp 的错误:

  File "d:/Griffin/Documents/Coding/Python/network.py", line 42, in <module>
    val = subprocess.Popen( [r"C:\Windows\System32\OpenSSH\sftp.exe"] )
  File "C:\Users\Griffin\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 775, in __init__
    restore_signals, start_new_session)
  File "C:\Users\Griffin\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 1178, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

Traceback (most recent call last):
  File "d:/Griffin/Documents/Coding/Python/network.py", line 44, in <module>
    val = subprocess.run( ["sftp"] )
  File "C:\Users\Griffin\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 472, in run
    with Popen(*popenargs, **kwargs) as process:
  File "C:\Users\Griffin\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 775, in __init__
    restore_signals, start_new_session)
  File "C:\Users\Griffin\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 1178, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

'sftp' is not recognized as an internal or external command,
operable program or batch file.
Traceback (most recent call last):
  File "d:/Griffin/Documents/Coding/Python/network.py", line 48, in <module>
    os.chdir( "C:\Windows\System32\OpenSSH" )
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C:\\Windows\\System32\\OpenSSH'

事实上,如果我进行 dir 打印,这些程序不会显示在列表中。

os.chdir( "C:\Windows\System32" )
val = os.system( "dir" )

print( val )

如果我在 OpenSSH 目录上尝试相同的操作,则无法完全找到它。

os.chdir( "C:\Windows\System32\OpenSSH" )
val = os.system( "dir" )

print( val )

正如您所期望的那样,所有这些在 python 之外都可以正常工作,并且它们也可以在 .bat 中工作。

标签: pythonwindows-subsystem-for-linux

解决方案


推荐阅读