【问题标题】:Calling defrag with subprocess.Popen on Windows 7在 Windows 7 上使用 subprocess.Popen 调用碎片整理
【发布时间】:2014-02-19 07:43:33
【问题描述】:

经过长时间运行后,我想使用Python subprocess.PopenWindows 7 上调用碎片整理工具defrag,如示例所示。

from subprocess import Popen

process1 = Popen('defrag', shell = True)
process1.wait()
print('finished defragmentation!')

这给出以下输出:

Der Befehl "defrag" ist entweder falsch geschrieben oder
konnte nicht gefunden werden.
finished defragmentation!

对于德国的输出感到抱歉。不知道怎么转英文。但是,这意味着找不到命令defrag。我尝试使用shell=False 并使用序列作为输入参数。没有任何效果,但我记得我在Windows XP 上做了同样的事情。也可以使用Popen 调用dir。为什么defrag 不适用于Windows 7?我怎样才能让它运行?

【问题讨论】:

  • 就像一个非常愚蠢的检查一样,如果你打开cmd 并写defrag,你得到的帮助文本是否正确?
  • 是的,当然。从命令行运行碎片整理工作。
  • 是的,再次抱歉这个愚蠢的问题。你能试试吗:Popen('Defrag.exe', shell = True, cwd='C:\\Windows\\System32\\')
  • 没有愚蠢的问题。但指定 cwd 也无济于事。错误信息是一样的。
  • 我现在正在胡思乱想,因为这应该可以工作。它都是正确的大写格式,路径是精确的。尝试以管理员身份运行?

标签: python windows-7 subprocess


【解决方案1】:

现在我可以回答我自己的问题了。问题是我在 64 位 Windows 机器上运行 32 位 Python。使用 64 位 Python 安装一切正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-12
    • 2021-04-29
    • 2012-05-12
    • 2014-04-29
    • 2013-11-19
    • 2012-03-18
    • 2012-02-22
    • 2018-08-07
    相关资源
    最近更新 更多