"mode" 不在 python 3.5 子进程中 运行
"mode" doesn't run in python 3.5 subprocess
我在从事自动化项目时遇到了一些难题。
当我尝试 运行:
program = subprocess.run("mode")
我得到:
FileNotFoundError: [WinError 2] The system cannot find the file specified
但是,当我用 ipconfig 替换模式时:
program = subprocess.run("ipconfig")
运行完全没问题。
有人解释一下吗?我目前正在使用批处理文件 运行 模式命令,但我想在不编辑批处理文件的情况下更改参数。
编辑 1:
我也刚刚尝试使用 os.system:
os.system("mode")
这也奏效了。
编辑 2:
现在我只想回答原来的问题,只是为了了解发生了什么。
在 Actual meaning of 'shell=True' in subprocess 中它几乎说 shell=True
是你应该回避的东西。
FileNotFoundError: [WinError 2] The system cannot find the file specified
是什么提示我您可能希望在您的子进程调用中 shell=True
。如果找不到文件,则意味着以下两种情况之一:
- 它不在你的路径上。
- 它实际上不是一个文件。
例如,在Linux中:
$ which echo
echo: shell built-in command
这很明显 是 没有 echo
文件。它只是 shell 中内置的一个命令。这 可能 与 Windows 上的 mode
相同。虽然 this site 似乎暗示它是一个 MODE.COM
文件。您可以尝试调用 that,如
subprocess.run('MODE.COM')
这可能有效 - 至少根据我链接到
的 the answers 之一
Invoking via the shell does allow you to expand environment variables and file globs according to the shell's usual mechanism. On POSIX systems, the shell expands file globs to a list of files. On Windows, a file glob (e.g., ".") is not expanded by the shell, anyway (but environment variables on a command line are expanded by cmd.exe).
所以在你的情况下,也许 mode
不是文件,但 MODE.COM
是 ,因为 Windows 有一个参差不齐的文件与大小写的关系,似乎有可能通过传递 shell=True
,Windows shell 愉快地接受 mode
并为您将其转换为 MODE.COM
,但没有它,它试图执行字面上名为 mode
的文件,该文件不存在。
我在从事自动化项目时遇到了一些难题。
当我尝试 运行:
program = subprocess.run("mode")
我得到:
FileNotFoundError: [WinError 2] The system cannot find the file specified
但是,当我用 ipconfig 替换模式时:
program = subprocess.run("ipconfig")
运行完全没问题。
有人解释一下吗?我目前正在使用批处理文件 运行 模式命令,但我想在不编辑批处理文件的情况下更改参数。
编辑 1:
我也刚刚尝试使用 os.system:
os.system("mode")
这也奏效了。
编辑 2:
现在我只想回答原来的问题,只是为了了解发生了什么。
在 Actual meaning of 'shell=True' in subprocess 中它几乎说 shell=True
是你应该回避的东西。
FileNotFoundError: [WinError 2] The system cannot find the file specified
是什么提示我您可能希望在您的子进程调用中 shell=True
。如果找不到文件,则意味着以下两种情况之一:
- 它不在你的路径上。
- 它实际上不是一个文件。
例如,在Linux中:
$ which echo
echo: shell built-in command
这很明显 是 没有 echo
文件。它只是 shell 中内置的一个命令。这 可能 与 Windows 上的 mode
相同。虽然 this site 似乎暗示它是一个 MODE.COM
文件。您可以尝试调用 that,如
subprocess.run('MODE.COM')
这可能有效 - 至少根据我链接到
的 the answers 之一Invoking via the shell does allow you to expand environment variables and file globs according to the shell's usual mechanism. On POSIX systems, the shell expands file globs to a list of files. On Windows, a file glob (e.g., ".") is not expanded by the shell, anyway (but environment variables on a command line are expanded by cmd.exe).
所以在你的情况下,也许 mode
不是文件,但 MODE.COM
是 ,因为 Windows 有一个参差不齐的文件与大小写的关系,似乎有可能通过传递 shell=True
,Windows shell 愉快地接受 mode
并为您将其转换为 MODE.COM
,但没有它,它试图执行字面上名为 mode
的文件,该文件不存在。