赞
踩
- >>> subprocess.run(["dir", "."])
- Traceback (most recent call last):
- File "<pyshell#3>", line 1, in <module>
- subprocess.run(["dir", "."])
- File "C:\Users\lenovo\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 693, in run
- with Popen(*popenargs, **kwargs) as process:
- File "C:\Users\lenovo\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 947, in __init__
- restore_signals, start_new_session)
- File "C:\Users\lenovo\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 1224, in _execute_child
- startupinfo)
- FileNotFoundError: [WinError 2] 系统找不到指定的文件。
- >>> subprocess.run(['dir'], shell = True)
- CompletedProcess(args=['dir'], returncode=0)
- >>> subprocess.run(['dir', '.'], shell = True, stdout = subprocess.PIPE)
- CompletedProcess(args=['dir', '.'], returncode=0, stdout=b' \xc7\xfd\xb6\xaf\xc6\xf7 C \xd6\xd0\xb5\xc4\xbe\xed\xca\xc7 Windows7_OS\r\n \xbe\xed\xb5\xc4\xd0\xf2\xc1\xd0\xba\xc5\xca\xc7 D835-8E7E\r\n\r\n C:\\Users\\lenovo\\AppData\\Local\\Programs\\Python\\Python35-32 \xb5\xc4\xc4\xbf\xc2\xbc\r\n\r\n2017/11/02 19:39 <DIR> .\r\n2017/11/02 19:39 <DIR> ..\r\n2016/12/11 17:07 <DIR> DLLs\r\n2016/12/11 17:07 <DIR> Doc\r\n2016/12/11 17:06 <DIR> include\r\n2016/12/11 17:07 <DIR> Lib\r\n2016/12/11 17:07 <DIR> libs\r\n2016/06/25 22:08 30,345 LICENSE.txt\r\n2016/06/25 21:48 340,667 NEWS.txt\r\n2016/06/25 22:02 39,576 python.exe\r\n2016/06/25 22:02 51,864 python3.dll\r\n2016/06/25 22:02 3,127,960 python35.dll\r\n2016/06/25 22:02 39,576 pythonw.exe\r\n2016/06/25 21:48 8,282 README.txt\r\n2017/10/17 13:02 <DIR> Scripts\r\n2016/12/11 17:08 <DIR> tcl\r\n2016/12/11 17:07 <DIR> Tools\r\n2016/03/17 22:48 85,840 vcruntime140.dll\r\n 8 \xb8\xf6\xce\xc4\xbc\xfe 3,724,110 \xd7\xd6\xbd\xda\r\n 10 \xb8\xf6\xc4\xbf\xc2\xbc 19,696,742,400 \xbf\xc9\xd3\xc3\xd7\xd6\xbd\xda\r\n')
Stackoverflow:
In Windows , to use echo in subprocess, you would need to use shell=True . This is because echo is not a separate executable, but rather a built-in command for the windows command line. Example -
process1 = subprocess.Popen(command1,stdout=subprocess.PIPE,shell=True)
Also, please do note, you should only use shell=True when absolutely necessary (as in this case , to use echo in windows in subprocess).
————————————————————————————————————————————————————————————
To support a wide variety of use cases, the Popen
constructor (and the convenience functions) accept a large number of optional arguments. For most typical use cases, many of these arguments can be safely left at their default values. The arguments that are most commonly needed are:
args is required for all calls and should be a string, or a sequence of program arguments. Providing a sequence of arguments is generally preferred, as it allows the module to take care of any required escaping and quoting of arguments (e.g. to permit spaces in file names). If passing a single string, either shell must be
True
(see below) or else the string must simply name the program to be executed without specifying any arguments.stdin, stdout and stderr specify the executed program’s standard input, standard output and standard error file handles, respectively. Valid values are
PIPE
,DEVNULL
, an existing file descriptor (a positive integer), an existing file object, andNone
.PIPE
indicates that a new pipe to the child should be created.DEVNULL
indicates that the special fileos.devnull
will be used. With the default settings ofNone
, no redirection will occur; the child’s file handles will be inherited from the parent. Additionally, stderr can beSTDOUT
, which indicates that the stderr data from the child process should be captured into the same file handle as for stdout.If universal_newlines is
False
the file objects stdin, stdout and stderr will be opened as binary streams, and no line ending conversion is done.If universal_newlines is
True
, these file objects will be opened as text streams in universal newlines mode using the encoding returned bylocale.getpreferredencoding(False)
. For stdin, line ending characters'\n'
in the input will be converted to the default line separatoros.linesep
. For stdout and stderr, all line endings in the output will be converted to'\n'
. For more information see the documentation of theio.TextIOWrapper
class when the newline argument to its constructor isNone
.Note
The newlines attribute of the file objects
Popen.stdin
,Popen.stdout
andPopen.stderr
are not updated by thePopen.communicate()
method.If shell is
True
, the specified command will be executed through the shell. This can be useful if you are using Python primarily for the enhanced control flow it offers over most system shells and still want convenient access to other shell features such as shell pipes, filename wildcards, environment variable expansion, and expansion of~
to a user’s home directory. However, note that Python itself offers implementations of many shell-like features (in particular,glob
,fnmatch
,os.walk()
,os.path.expandvars()
,os.path.expanduser()
, andshutil
).Changed in version 3.3: When universal_newlines is
True
, the class uses the encodinglocale.getpreferredencoding(False)
instead oflocale.getpreferredencoding()
. See theio.TextIOWrapper
class for more information on this change.Note
Read the Security Considerations section before using
shell=True
.
__________________________________________________________________________________________________________________________________________
GB2312是中国规定的汉字编码,也可以说是简体中文的字符集编码
GBK 是 GB2312的扩展 ,除了兼容GB2312外,它还能显示繁体中文,还有日文的假名
cp936:中文本地系统是Windows中的cmd,默认codepage是CP936,cp936就是指系统里第936号编码格式,即GB2312的编码。
(当然有其它编码格式:cp950 繁体中文、cp932 日语、cp1250 中欧语言。。。)
Unicode是国际组织制定的可以容纳世界上所有文字和符号的字符编码方案。UTF-8、UTF-16、UTF-32都是将数字转换到程序数据的编码方案。
UTF-8 (8-bit Unicode Transformation Format)是最流行的一种对 Unicode 进行传播和存储的编码方式。它用不同的 bytes 来表示每一个代码点。ASCII 字符每个只需要用一个 byte ,与 ASCII 的编码是一样的。所以说 ASCII 是 UTF-8 的一个子集。
- >>> import locale
- >>> locale.getpreferredencoding(False)
- 'cp936'
- >>> import subprocess
- >>> subprocess.run(['dir'], shell = True, stdout = subprocess.PIPE)
- CompletedProcess(args=['dir'], returncode=0, stdout=b' \xc7\xfd\xb6\xaf\xc6\xf7 C \xd6\xd0\xb5\xc4\xbe\xed\xca\xc7 Windows7_OS\r\n \xbe\xed\xb5\xc4\xd0\xf2\xc1\xd0\xba\xc5\xca\xc7 D835-8E7E\r\n\r\n C:\\Users\\lenovo\\AppData\\Local\\Programs\\Python\\Python35-32 \xb5\xc4\xc4\xbf\xc2\xbc\r\n\r\n2017/11/02 19:39 <DIR> .\r\n2017/11/02 19:39 <DIR> ..\r\n2016/12/11 17:07 <DIR> DLLs\r\n2016/12/11 17:07 <DIR> Doc\r\n2016/12/11 17:06 <DIR> include\r\n2016/12/11 17:07 <DIR> Lib\r\n2016/12/11 17:07 <DIR> libs\r\n2016/06/25 22:08 30,345 LICENSE.txt\r\n2016/06/25 21:48 340,667 NEWS.txt\r\n2016/06/25 22:02 39,576 python.exe\r\n2016/06/25 22:02 51,864 python3.dll\r\n2016/06/25 22:02 3,127,960 python35.dll\r\n2016/06/25 22:02 39,576 pythonw.exe\r\n2016/06/25 21:48 8,282 README.txt\r\n2017/10/17 13:02 <DIR> Scripts\r\n2016/12/11 17:08 <DIR> tcl\r\n2016/12/11 17:07 <DIR> Tools\r\n2016/03/17 22:48 85,840 vcruntime140.dll\r\n 8 \xb8\xf6\xce\xc4\xbc\xfe 3,724,110 \xd7\xd6\xbd\xda\r\n 10 \xb8\xf6\xc4\xbf\xc2\xbc 19,673,907,200 \xbf\xc9\xd3\xc3\xd7\xd6\xbd\xda\r\n')
- >>> print(b' \xc7\xfd\xb6\xaf\xc6\xf7 C \xd6\xd0\xb5\xc4\xbe\xed\xca\xc7 Windows7_OS\r\n \xbe\xed\xb5\xc4\xd0\xf2\xc1\xd0\xba\xc5\xca\xc7 D835-8E7E\r\n\r\n C:\\Users\\lenovo\\AppData\\Local\\Programs\\Python\\Python35-32 \xb5\xc4\xc4\xbf\xc2\xbc\r\n\r\n2017/11/02 19:39 <DIR> .\r\n2017/11/02 19:39 <DIR> ..\r\n2016/12/11 17:07 <DIR> DLLs\r\n2016/12/11 17:07 <DIR> Doc\r\n2016/12/11 17:06 <DIR> include\r\n2016/12/11 17:07 <DIR> Lib\r\n2016/12/11 17:07 <DIR> libs\r\n2016/06/25 22:08 30,345 LICENSE.txt\r\n2016/06/25 21:48 340,667 NEWS.txt\r\n2016/06/25 22:02 39,576 python.exe\r\n2016/06/25 22:02 51,864 python3.dll\r\n2016/06/25 22:02 3,127,960 python35.dll\r\n2016/06/25 22:02 39,576 pythonw.exe\r\n2016/06/25 21:48 8,282 README.txt\r\n2017/10/17 13:02 <DIR> Scripts\r\n2016/12/11 17:08 <DIR> tcl\r\n2016/12/11 17:07 <DIR> Tools\r\n2016/03/17 22:48 85,840 vcruntime140.dll\r\n 8 \xb8\xf6\xce\xc4\xbc\xfe 3,724,110 \xd7\xd6\xbd\xda\r\n 10 \xb8\xf6\xc4\xbf\xc2\xbc 19,673,907,200 \xbf\xc9\xd3\xc3\xd7\xd6\xbd\xda\r\n'.decode('cp936'))
- 驱动器 C 中的卷是 Windows7_OS
-
- 卷的序列号是 D835-8E7E
-
-
-
- C:\Users\lenovo\AppData\Local\Programs\Python\Python35-32 的目录
-
-
-
- 2017/11/02 19:39 <DIR> .
-
- 2017/11/02 19:39 <DIR> ..
-
- 2016/12/11 17:07 <DIR> DLLs
-
- 2016/12/11 17:07 <DIR> Doc
-
- 2016/12/11 17:06 <DIR> include
-
- 2016/12/11 17:07 <DIR> Lib
-
- 2016/12/11 17:07 <DIR> libs
-
- 2016/06/25 22:08 30,345 LICENSE.txt
-
- 2016/06/25 21:48 340,667 NEWS.txt
-
- 2016/06/25 22:02 39,576 python.exe
-
- 2016/06/25 22:02 51,864 python3.dll
-
- 2016/06/25 22:02 3,127,960 python35.dll
-
- 2016/06/25 22:02 39,576 pythonw.exe
-
- 2016/06/25 21:48 8,282 README.txt
-
- 2017/10/17 13:02 <DIR> Scripts
-
- 2016/12/11 17:08 <DIR> tcl
-
- 2016/12/11 17:07 <DIR> Tools
-
- 2016/03/17 22:48 85,840 vcruntime140.dll
-
- 8 个文件 3,724,110 字节
-
- 10 个目录 19,673,907,200 可用字节
-
-
- >>> subprocess.run(['dir'], shell = True, stdout = subprocess.PIPE, universal_newlines = True)
- CompletedProcess(args=['dir'], returncode=0, stdout=' 驱动器 C 中的卷是 Windows7_OS\n 卷的序列号是 D835-8E7E\n\n C:\\Users\\lenovo\\AppData\\Local\\Programs\\Python\\Python35-32 的目录\n\n2017/11/02 19:39 <DIR> .\n2017/11/02 19:39 <DIR> ..\n2016/12/11 17:07 <DIR> DLLs\n2016/12/11 17:07 <DIR> Doc\n2016/12/11 17:06 <DIR> include\n2016/12/11 17:07 <DIR> Lib\n2016/12/11 17:07 <DIR> libs\n2016/06/25 22:08 30,345 LICENSE.txt\n2016/06/25 21:48 340,667 NEWS.txt\n2016/06/25 22:02 39,576 python.exe\n2016/06/25 22:02 51,864 python3.dll\n2016/06/25 22:02 3,127,960 python35.dll\n2016/06/25 22:02 39,576 pythonw.exe\n2016/06/25 21:48 8,282 README.txt\n2017/10/17 13:02 <DIR> Scripts\n2016/12/11 17:08 <DIR> tcl\n2016/12/11 17:07 <DIR> Tools\n2016/03/17 22:48 85,840 vcruntime140.dll\n 8 个文件 3,724,110 字节\n 10 个目录 19,673,907,200 可用字节\n')
- >>> print(' 驱动器 C 中的卷是 Windows7_OS\n 卷的序列号是 D835-8E7E\n\n C:\\Users\\lenovo\\AppData\\Local\\Programs\\Python\\Python35-32 的目录\n\n2017/11/02 19:39 <DIR> .\n2017/11/02 19:39 <DIR> ..\n2016/12/11 17:07 <DIR> DLLs\n2016/12/11 17:07 <DIR> Doc\n2016/12/11 17:06 <DIR> include\n2016/12/11 17:07 <DIR> Lib\n2016/12/11 17:07 <DIR> libs\n2016/06/25 22:08 30,345 LICENSE.txt\n2016/06/25 21:48 340,667 NEWS.txt\n2016/06/25 22:02 39,576 python.exe\n2016/06/25 22:02 51,864 python3.dll\n2016/06/25 22:02 3,127,960 python35.dll\n2016/06/25 22:02 39,576 pythonw.exe\n2016/06/25 21:48 8,282 README.txt\n2017/10/17 13:02 <DIR> Scripts\n2016/12/11 17:08 <DIR> tcl\n2016/12/11 17:07 <DIR> Tools\n2016/03/17 22:48 85,840 vcruntime140.dll\n 8 个文件 3,724,110 字节\n 10 个目录 19,673,907,200 可用字节\n')
- 驱动器 C 中的卷是 Windows7_OS
- 卷的序列号是 D835-8E7E
-
- C:\Users\lenovo\AppData\Local\Programs\Python\Python35-32 的目录
-
- 2017/11/02 19:39 <DIR> .
- 2017/11/02 19:39 <DIR> ..
- 2016/12/11 17:07 <DIR> DLLs
- 2016/12/11 17:07 <DIR> Doc
- 2016/12/11 17:06 <DIR> include
- 2016/12/11 17:07 <DIR> Lib
- 2016/12/11 17:07 <DIR> libs
- 2016/06/25 22:08 30,345 LICENSE.txt
- 2016/06/25 21:48 340,667 NEWS.txt
- 2016/06/25 22:02 39,576 python.exe
- 2016/06/25 22:02 51,864 python3.dll
- 2016/06/25 22:02 3,127,960 python35.dll
- 2016/06/25 22:02 39,576 pythonw.exe
- 2016/06/25 21:48 8,282 README.txt
- 2017/10/17 13:02 <DIR> Scripts
- 2016/12/11 17:08 <DIR> tcl
- 2016/12/11 17:07 <DIR> Tools
- 2016/03/17 22:48 85,840 vcruntime140.dll
- 8 个文件 3,724,110 字节
- 10 个目录 19,673,907,200 可用字节
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。