子进程中的'shell'参数在Windows上意味着什么?

时间:2021-12-30 20:58:41

The docs for the subprocess module state that 'If shell is True, the specified command will be executed through the shell'. What does this mean in practice, on a Windows OS?

子进程模块的文档声明'如果shell为True,则指定的命令将通过shell执行'。在Windows操作系统中,这在实践中意味着什么?

4 个解决方案

#1


When you execute an external process, the command you want may look something like "foo arg1 arg2 arg3". If "foo" is an executable, that is what gets executed and given the arguments.

当您执行外部进程时,您想要的命令可能类似于“foo arg1 arg2 arg3”。如果“foo”是可执行文件,那就是执行并给出参数的内容。

However, often it is the case that "foo" is actually a script of some sort, or maybe a command that is built-in to the shell and not an actual executable file on disk. In this case the system can't execute "foo" directly because, strictly speaking, these sorts of things aren't executable. They need some sort of "shell" to execute them. On *nix systems this shell is typically (but not necessarily) /bin/sh. On windows it will typically be cmd.exe (or whatever is stored in the COMSPEC environment variable).

但是,通常情况下“foo”实际上是某种类型的脚本,或者可能是内置于shell的命令而不是磁盘上的实际可执行文件。在这种情况下,系统不能直接执行“foo”,因为严格来说,这些类型的东西是不可执行的。他们需要某种“shell”来执行它们。在* nix系统上,此shell通常(但不一定)/ bin / sh。在Windows上,它通常是cmd.exe(或存储在COMSPEC环境变量中的任何内容)。

This parameter lets you define what shell you wish to use to execute your command, for the relatively rare case when you don't want the default.

此参数允许您定义要用于执行命令的shell,对于不需要默认值的相对罕见的情况。

#2


It means that the command will be executed using the program specified in the COMSPEC environment variable. Usually cmd.exe.

这意味着将使用COMSPEC环境变量中指定的程序执行该命令。通常是cmd.exe。

To be exact, subprocess calls the CreateProcess windows api function, passing "cmd.exe /c " + args as the lpCommandLine argument.

确切地说,子进程调用CreateProcess windows api函数,将“cmd.exe / c”+ args作为lpCommandLine参数传递。

If shell==False, the lpCommandLine argument to CreateProcess is simply args.

如果shell == False,则CreateProcess的lpCommandLine参数只是args。

#3


In addition to what was said in other answers, it is useful in practice if you want to open a file in the default viewer for that file type. For instance, if you want to open an HTML or PDF file, but will not know which browser or viewer is installed on the systems it will be run on, or have no guarantees as to the path to the executable, you can simply pass the file name as the only argument for the args field, then set shell=True. This will have Windows use whatever program is associated with that file type. One caveat, if the path to your file has spaces, you need to surround it with two ".

除了在其他答案中所说的内容之外,如果要在默认查看器中为该文件类型打开文件,则在实践中很有用。例如,如果要打开HTML或PDF文件,但不知道系统上将安装哪个浏览器或查看器,或者无法保证可执行文件的路径,则可以简单地通过文件名作为args字段的唯一参数,然后设置shell = True。这将使Windows使用与该文件类型相关联的任何程序。需要注意的是,如果文件的路径有空格,则需要用两个“。

eg.

path = "C:\\Documents and Settings\\Bob\\Desktop\\New Folder\\README.txt"
subprocess.call('""' + path + '""', shell = True)

#4


In using-the-subprocess-module, there is an explicit paragraph:

在using-the-subprocess-module中,有一个明确的段落:

The executable argument specifies the program to execute. It is very seldom needed: Usually, the program to execute is defined by the args argument. If shell=True, the executable argument specifies which shell to use. On Unix, the default shell is /bin/sh. On Windows, the default shell is specified by the COMSPEC environment variable.

可执行参数指定要执行的程序。它很少需要:通常,要执行的程序由args参数定义。如果shell = True,则可执行参数指定要使用的shell。在Unix上,默认shell是/ bin / sh。在Windows上,默认shell由COMSPEC环境变量指定。

Windows example - the shell (cmd.exe) command date -t will not be recognized without the shell:

Windows示例 - 如果没有shell,将无法识别shell(cmd.exe)命令date -t​​:

>>> p=subprocess.Popen(["date", "/t"], stdout=subprocess.PIPE)
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "C:\Python26\lib\subprocess.py", line 595, in __init__
    errread, errwrite)
  File "C:\Python26\lib\subprocess.py", line 804, in _execute_child
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
>>> 

Using a shell, all is well:

使用shell,一切都很好:

>>> p=subprocess.Popen(["date", "/t"], shell=True, stdout=subprocess.PIPE)
>>> p.communicate()
('Wed 04/22/2009 \r\n', None)
>>>

#1


When you execute an external process, the command you want may look something like "foo arg1 arg2 arg3". If "foo" is an executable, that is what gets executed and given the arguments.

当您执行外部进程时,您想要的命令可能类似于“foo arg1 arg2 arg3”。如果“foo”是可执行文件,那就是执行并给出参数的内容。

However, often it is the case that "foo" is actually a script of some sort, or maybe a command that is built-in to the shell and not an actual executable file on disk. In this case the system can't execute "foo" directly because, strictly speaking, these sorts of things aren't executable. They need some sort of "shell" to execute them. On *nix systems this shell is typically (but not necessarily) /bin/sh. On windows it will typically be cmd.exe (or whatever is stored in the COMSPEC environment variable).

但是,通常情况下“foo”实际上是某种类型的脚本,或者可能是内置于shell的命令而不是磁盘上的实际可执行文件。在这种情况下,系统不能直接执行“foo”,因为严格来说,这些类型的东西是不可执行的。他们需要某种“shell”来执行它们。在* nix系统上,此shell通常(但不一定)/ bin / sh。在Windows上,它通常是cmd.exe(或存储在COMSPEC环境变量中的任何内容)。

This parameter lets you define what shell you wish to use to execute your command, for the relatively rare case when you don't want the default.

此参数允许您定义要用于执行命令的shell,对于不需要默认值的相对罕见的情况。

#2


It means that the command will be executed using the program specified in the COMSPEC environment variable. Usually cmd.exe.

这意味着将使用COMSPEC环境变量中指定的程序执行该命令。通常是cmd.exe。

To be exact, subprocess calls the CreateProcess windows api function, passing "cmd.exe /c " + args as the lpCommandLine argument.

确切地说,子进程调用CreateProcess windows api函数,将“cmd.exe / c”+ args作为lpCommandLine参数传递。

If shell==False, the lpCommandLine argument to CreateProcess is simply args.

如果shell == False,则CreateProcess的lpCommandLine参数只是args。

#3


In addition to what was said in other answers, it is useful in practice if you want to open a file in the default viewer for that file type. For instance, if you want to open an HTML or PDF file, but will not know which browser or viewer is installed on the systems it will be run on, or have no guarantees as to the path to the executable, you can simply pass the file name as the only argument for the args field, then set shell=True. This will have Windows use whatever program is associated with that file type. One caveat, if the path to your file has spaces, you need to surround it with two ".

除了在其他答案中所说的内容之外,如果要在默认查看器中为该文件类型打开文件,则在实践中很有用。例如,如果要打开HTML或PDF文件,但不知道系统上将安装哪个浏览器或查看器,或者无法保证可执行文件的路径,则可以简单地通过文件名作为args字段的唯一参数,然后设置shell = True。这将使Windows使用与该文件类型相关联的任何程序。需要注意的是,如果文件的路径有空格,则需要用两个“。

eg.

path = "C:\\Documents and Settings\\Bob\\Desktop\\New Folder\\README.txt"
subprocess.call('""' + path + '""', shell = True)

#4


In using-the-subprocess-module, there is an explicit paragraph:

在using-the-subprocess-module中,有一个明确的段落:

The executable argument specifies the program to execute. It is very seldom needed: Usually, the program to execute is defined by the args argument. If shell=True, the executable argument specifies which shell to use. On Unix, the default shell is /bin/sh. On Windows, the default shell is specified by the COMSPEC environment variable.

可执行参数指定要执行的程序。它很少需要:通常,要执行的程序由args参数定义。如果shell = True,则可执行参数指定要使用的shell。在Unix上,默认shell是/ bin / sh。在Windows上,默认shell由COMSPEC环境变量指定。

Windows example - the shell (cmd.exe) command date -t will not be recognized without the shell:

Windows示例 - 如果没有shell,将无法识别shell(cmd.exe)命令date -t​​:

>>> p=subprocess.Popen(["date", "/t"], stdout=subprocess.PIPE)
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "C:\Python26\lib\subprocess.py", line 595, in __init__
    errread, errwrite)
  File "C:\Python26\lib\subprocess.py", line 804, in _execute_child
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
>>> 

Using a shell, all is well:

使用shell,一切都很好:

>>> p=subprocess.Popen(["date", "/t"], shell=True, stdout=subprocess.PIPE)
>>> p.communicate()
('Wed 04/22/2009 \r\n', None)
>>>