【问题标题】:Subprocess check_output(['dir']) favoring Linux binaries支持 Linux 二进制文件的子进程 check_output(['dir'])
【发布时间】:2016-05-15 20:47:06
【问题描述】:

我想在 shell 中查看 Windows 的 dir 命令的内容。我安装了 Cygwin 和 mingw。有了这段代码,

import subprocess as sub
print sub.check_output(['dir'])

它被解析为

DCcircuits.py
IDLE\ (idle.pyw).lnk
Newpy.bat
idle.bat\ -\ Shortcut.lnk
idle.pyw\ -\ Shortcut.lnk
indexProgramFiles.py
maps\ traveler
orderedDict.txt
pipfreeze.txt
remote\ wireless
tracertIps.py

我正在尝试显示这样的输出。

 Volume in drive C is TI10693600D
 Volume Serial Number is 240D-7F0A

 Directory of C:\Users\Clayton

02/03/2016  10:22 AM    <DIR>          .
02/03/2016  10:22 AM    <DIR>          ..
11/05/2015  03:07 PM    <DIR>          .android
08/14/2015  12:02 PM    <DIR>          .idlerc
11/01/2015  10:38 PM    <DIR>          .jmc
01/04/2016  09:33 AM    <DIR>          .matplotlib
01/08/2016  10:18 AM    <DIR>          .oracle_jre_usage
09/04/2015  02:43 PM    <DIR>          3D Objects
01/29/2016  12:06 PM    <DIR>          Contacts
02/05/2016  10:35 AM    <DIR>          Desktop
02/04/2016  08:38 PM    <DIR>          Documents
02/04/2016  04:31 PM    <DIR>          Downloads
01/29/2016  12:06 PM    <DIR>          Favorites
02/04/2016  05:25 PM    <DIR>          Google Drive
01/29/2016  12:06 PM    <DIR>          Links
01/29/2016  12:06 PM    <DIR>          Music
12/24/2015  03:10 PM    <DIR>          OneDrive
02/03/2016  03:36 PM    <DIR>          Pictures
01/29/2016  12:06 PM    <DIR>          Saved Games
01/29/2016  12:06 PM    <DIR>          Searches
11/28/2014  10:14 PM    <DIR>          Shared
04/12/2015  03:23 PM    <DIR>          Tracing
01/29/2016  12:06 PM    <DIR>          Videos
               0 File(s)              0 bytes
              26 Dir(s)  480,478,224,384 bytes free

当我从path中取出cygwin和mingw时,会出现这个错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\subprocess.py", line 566, in check_output
    process = Popen(stdout=PIPE, *popenargs, **kwargs)
  File "C:\Python27\lib\subprocess.py", line 710, in __init__
    errread, errwrite)
  File "C:\Python27\lib\subprocess.py", line 958, in _execute_child
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified

有没有办法使用 CMD 代替 Cygwin 命令?

提前致谢。

【问题讨论】:

    标签: python windows python-2.7 format subprocess


    【解决方案1】:

    shell=True 传递给check_output 似乎有效。我怀疑dir 是作为内置的shell (cmd) 而不是独立的可执行文件实现的。

    【讨论】:

    • 谢谢。它试图执行它并且只找到dir.exe 的cywin 版本。现在shell 修复它。
    • @ClaytonWahlstrom dir cmd's internal command 和 cygwin 的 dir 是独立的(但相关:都显示文件列表)命令。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-22
    • 2018-01-03
    • 2016-07-10
    • 1970-01-01
    • 2010-09-08
    • 1970-01-01
    相关资源
    最近更新 更多