subprocess运行windows命令行python

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

stdinstdout and stderr specify the executed program’s standard input, standard output and standard error file handles, respectively. Valid values are PIPEDEVNULL, an existing file descriptor (a positive integer), an existing file object, and NonePIPE indicates that a new pipe to the child should be created. DEVNULL indicates that the special file os.devnull will be used. With the default settings of None, no redirection will occur; the child’s file handles will be inherited from the parent. Additionally, stderr can be STDOUT, 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 stdinstdout 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 by locale.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 the io.TextIOWrapper class when the newline argument to its constructor is None.

Note

 

The newlines attribute of the file objects Popen.stdinPopen.stdout and Popen.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, globfnmatchos.walk()os.path.expandvars()os.path.expanduser(), and shutil).

Changed in version 3.3: When universal_newlines is True, the class uses the encoding locale.getpreferredencoding(False) instead of locale.getpreferredencoding(). See the io.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 可用字节



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值