繁体   English   中英

Python subprocess.call 在没有 shell=True 的 Windows 上失败

[英]Python subprocess.call fails on windows without shell=True

我在 Windows 10 64 位上使用Python 2.7.14 我需要使用

子进程调用

但如果我不通过shell=True ,它每次都会失败。 根据python doc ,不应使用shell=True 我也想知道为什么它失败了。

python
ActivePython 2.7.14.2717 (ActiveState Software Inc.) based on
Python 2.7.14 (default, Dec 15 2017, 16:31:45) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.call(['date'])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\subprocess.py", line 168, in call
    return Popen(*popenargs, **kwargs).wait()
  File "C:\Python27\lib\subprocess.py", line 390, in __init__
    errread, errwrite)
  File "C:\Python27\lib\subprocess.py", line 640, in _execute_child
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified

但是当使用 shell=True 运行时,它运行良好

>>> subprocess.call(['date'],shell=True)
The current date is: Wed 12/04/2019
Enter the new date: (mm-dd-yy)

我也在 mac 上试过,subprocess.call 在没有shell=True 的情况下运行良好

请帮忙。

date不是可执行文件,而是cmd.exe内置函数 您不能在外壳外使用它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM