简体   繁体   English

使用python脚本删除Windows Temp文件

[英]Deleting Windows Temp Files using python script

Can you help me how can i delete all the files under the Windows/Temp files?? 你能帮我一下如何删除Windows / Temp文件下的所有文件? Below are my scripts but it doesn't work at all. 下面是我的脚本但它根本不起作用。

import os
import subprocess
recPath = 'C:\\Windows\\Temp'
ls = []
if os.path.exists(recPath):
    for i in os.listdir(recPath):
        ls.append(os.path.join(recPath, i))
else:
    print 'Please provide valid path!'

paths = ' '.join(ls)
pObj = subprocess.Popen('rmdir C:\\Windows\\Temp\\*.* /s /q *.*'+paths, shell=True, stdout = subprocess.PIPE, stderr= subprocess.PIPE)
rTup = pObj.communicate()
rCod = pObj.returncode
if rCod == 0:
    print 'Success: Cleaned Windows Temp Folder'
else:
    print 'Fail: Unable to Clean Windows Temp Folder'

Thank you in advance. 先感谢您。

using windows command del to remove all files in dir with wildcard . 使用windows命令del删除带有通配符的dir中的所有文件。 This will delete all files recursively within it, however it will leave the empty subfolder there 这将在其中递归删除所有文件,但它会将空子文件夹留在那里

import os, subprocess
del_dir = r'c:\windows\temp'
pObj = subprocess.Popen('del /S /Q /F %s\\*.*' % del_dir, shell=True, stdout = subprocess.PIPE, stderr= subprocess.PIPE)
rTup = pObj.communicate()
rCod = pObj.returncode
if rCod == 0:
    print 'Success: Cleaned Windows Temp Folder'
else:
    print 'Fail: Unable to Clean Windows Temp Folder'

change the 1st line to below to delete whole directory tree of Windows\\Temp.This will remove everything include the Temp folder itself if success, recreate parent directory afterwards 将第1行更改为以下删除Windows \\ Temp的整个目录树。如果成功,将删除包含Temp文件夹本身的所有内容,之后重新创建父目录

del_dir = r'c:\windows\temp'
pObj = subprocess.Popen('rmdir /S /Q %s' % del_dir, shell=True, stdout = subprocess.PIPE, stderr= subprocess.PIPE)
# recreate the deleted parent dir in case it get deleted
os.makedirs(del_dir)

Else, rmtree from shutil should be a pretty good choice, ignore_errors set to ignore all the errors in middle and continue until all directory tree complete 另外,来自shutil的rmtree应该是一个不错的选择,ignore_errors设置为忽略中间的所有错误并继续直到所有目录树完成

import shutil, os
del_dir = r'c:\windows\temp'
shutil.rmtree(del_dir, ignore_errors=True)
# recreate the deleted parent dir in case it get deleted
os.makedirs(del_dir)

Another option to iterate over directory to be deleted 迭代目录以删除的另一个选项

import os,shutil
del_dir = r'c:\windows\temp'
for f in os.listdir(del_dir):
    if os.path.isfile(f):
        os.remove(f)
    elif os.path.isdir(f)
        shutil.rmtree(f, ignore_errors=True)

change the del_dir accordingly to any directory of interest 相应地将del_dir更改为任何感兴趣的目录

You are dealing with windows folder, beware to set the directory to delete carefully, you would not want to mistakenly put del_dir = r'c:\\windows' 你正在处理windows文件夹,小心设置要仔细删除的目录,你不要错误地把del_dir = r'c:\\ windows'

Use shutil . 使用shutil

import shutil

shutil.rmtree(r"C:\Windows\Temp")

You might want to hard-code the path. 您可能想要对路径进行硬编码。

    import os
    import shutil
    del_dir = r'C:\Windows\Temp'
    for f in os.listdir(del_dir):
        if os.path.isfile(r'C:\Windows\Temp\\'+f):
            os.remove(r'C:\Windows\Temp\\'+f)
        elif os.path.isdir(r'C:\Windows\Temp\\'+f):
            shutil.rmtree(r'C:\Windows\Temp\\'+f, ignore_errors=True)

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

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