当前位置:   article > 正文

subprocess运行windows命令行python_subprocess windows命令处理程序

subprocess windows命令处理程序
  1. >>> subprocess.run(["dir", "."])
  2. Traceback (most recent call last):
  3. File "<pyshell#3>", line 1, in <module>
  4. subprocess.run(["dir", "."])
  5. File "C:\Users\lenovo\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 693, in run
  6. with Popen(*popenargs, **kwargs) as process:
  7. File "C:\Users\lenovo\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 947, in __init__
  8. restore_signals, start_new_session)
  9. File "C:\Users\lenovo\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 1224, in _execute_child
  10. startupinfo)
  11. FileNotFoundError: [WinError 2] 系统找不到指定的文件。
  12. >>> subprocess.run(['dir'], shell = True)
  13. CompletedProcess(args=['dir'], returncode=0)
  14. >>> subprocess.run(['dir', '.'], shell = True, stdout = subprocess.PIPE)
  15. 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 的一个子集。

 

  1. >>> import locale
  2. >>> locale.getpreferredencoding(False)
  3. 'cp936'
  4. >>> import subprocess
  5. >>> subprocess.run(['dir'], shell = True, stdout = subprocess.PIPE)
  6. 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')
  7. >>> 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'))
  8. 驱动器 C 中的卷是 Windows7_OS
  9. 卷的序列号是 D835-8E7E
  10. C:\Users\lenovo\AppData\Local\Programs\Python\Python35-32 的目录
  11. 2017/11/02 19:39 <DIR> .
  12. 2017/11/02 19:39 <DIR> ..
  13. 2016/12/11 17:07 <DIR> DLLs
  14. 2016/12/11 17:07 <DIR> Doc
  15. 2016/12/11 17:06 <DIR> include
  16. 2016/12/11 17:07 <DIR> Lib
  17. 2016/12/11 17:07 <DIR> libs
  18. 2016/06/25 22:08 30,345 LICENSE.txt
  19. 2016/06/25 21:48 340,667 NEWS.txt
  20. 2016/06/25 22:02 39,576 python.exe
  21. 2016/06/25 22:02 51,864 python3.dll
  22. 2016/06/25 22:02 3,127,960 python35.dll
  23. 2016/06/25 22:02 39,576 pythonw.exe
  24. 2016/06/25 21:48 8,282 README.txt
  25. 2017/10/17 13:02 <DIR> Scripts
  26. 2016/12/11 17:08 <DIR> tcl
  27. 2016/12/11 17:07 <DIR> Tools
  28. 2016/03/17 22:48 85,840 vcruntime140.dll
  29. 8 个文件 3,724,110 字节
  30. 10 个目录 19,673,907,200 可用字节
  31. >>> subprocess.run(['dir'], shell = True, stdout = subprocess.PIPE, universal_newlines = True)
  32. 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')
  33. >>> 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')
  34. 驱动器 C 中的卷是 Windows7_OS
  35. 卷的序列号是 D835-8E7E
  36. C:\Users\lenovo\AppData\Local\Programs\Python\Python35-32 的目录
  37. 2017/11/02 19:39 <DIR> .
  38. 2017/11/02 19:39 <DIR> ..
  39. 2016/12/11 17:07 <DIR> DLLs
  40. 2016/12/11 17:07 <DIR> Doc
  41. 2016/12/11 17:06 <DIR> include
  42. 2016/12/11 17:07 <DIR> Lib
  43. 2016/12/11 17:07 <DIR> libs
  44. 2016/06/25 22:08 30,345 LICENSE.txt
  45. 2016/06/25 21:48 340,667 NEWS.txt
  46. 2016/06/25 22:02 39,576 python.exe
  47. 2016/06/25 22:02 51,864 python3.dll
  48. 2016/06/25 22:02 3,127,960 python35.dll
  49. 2016/06/25 22:02 39,576 pythonw.exe
  50. 2016/06/25 21:48 8,282 README.txt
  51. 2017/10/17 13:02 <DIR> Scripts
  52. 2016/12/11 17:08 <DIR> tcl
  53. 2016/12/11 17:07 <DIR> Tools
  54. 2016/03/17 22:48 85,840 vcruntime140.dll
  55. 8 个文件 3,724,110 字节
  56. 10 个目录 19,673,907,200 可用字节



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/218925
推荐阅读
相关标签
  

闽ICP备14008679号