磁盘碎片整理和磁盘清理使用Python脚本 [英] Disk Defrag and Disk Clean up using Python Script

查看:349
本文介绍了磁盘碎片整理和磁盘清理使用Python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能帮我解决一下如何使这个脚本工作。

Can you help me on how to make this script work.

对于碎片整理

import os;
defragmentation=os.popen('defrag.exe /C').read()
print(defragmentation);

磁盘清理

import os;
clean=os.popen('cleanmgr.exe /sagerun:1').read()
print(clean);

尝试此脚本时,它没有做任何事情,也没有错误消息提示。谢谢。

Upon trying this scripts, it didnt do anything and no error message prompt. Thank you.

推荐答案


  • 如果您的 defrag.exe cleanmgr.exe 不在您的路径中,它们将无法执行,您将无法获得错误消息

  • 您需要以管理员身份运行脚本才能使其正常工作。

  • 您无需终止行Python中的分号

    • If your defrag.exe or cleanmgr.exe are not in your path, they won't execute and you won't get an error message
    • You would need to run the scripts as an administrator to make them work.
    • You don't need to terminate your lines with a semi-colon in Python
    • 如果要查找可执行文件的正确完整路径,可以使用以下脚本:

      If you want to find the the correct full path of your executable, you can use the following script:

      paths = os.getenv('path').split(';')
      path_defrag = ''
      
      for p in paths:
          if os.access(os.path.join(p, 'defrag.exe'), os.X_OK):
              path_defrag = os.path.join(p, 'defrag.exe')
              break
      if not path_defrag:
          print('defrag.exe is not in your path or cannot be executed')
      




      • 我建议Spectras的想法并阅读输出w一旦一切都完成,它就来了。

      • 另外我认为Python在这里是一种矫枉过正,一个简单的批处理文件就可以完成这项工作

      • 这篇关于磁盘碎片整理和磁盘清理使用Python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆