Вопрос по unix, python – Удалить все файлы в каталоге

34

Попытка удалить все файлы в определенном каталоге приводит к следующей ошибке:

OSError: [Errno 2] No such file or directory: '/home/me/test/*'

Код, который я запускаю:

import os
test = "/home/me/test/*"
os.remove(test)
Error: User Rate Limit Exceededdocs.python.org/library/os.html#os.walk sunqiang

Ваш Ответ

11   ответов
0

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
dir = os.path.join(BASE_DIR,"foldername")

for file in files:
path = os.path.join(dir,file)
os.remove(path)
0

#python 2.7
import tempfile
import shutil
import exceptions
import os

def TempCleaner():
    temp_dir_name = tempfile.gettempdir()
    for currentdir in os.listdir(temp_dir_name):
        try:
           shutil.rmtree(os.path.join(temp_dir_name, currentdir))
        except exceptions.WindowsError, e:
            print u'Не удалось удалить:'+ e.filename
4

# python 2.7
import os

d='/home/me/test'
filesToRemove = [os.path.join(d,f) for f in os.listdir(d)]
for f in filesToRemove:
    os.remove(f) 
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceededos.remove(f) for f in filesError: User Rate Limit Exceeded
Error: User Rate Limit Exceededos.remove()Error: User Rate Limit Exceededos.remove(os.path.join("/home/me/test", f))Error: User Rate Limit Exceeded
Error: User Rate Limit ExceededosError: User Rate Limit ExceededfilesToRemove = [f for f in os.listdir(self.my_folder)] for f in filesToRemove: os.remove(os.path.join(self.my_folder, f))Error: User Rate Limit Exceeded
7

0

It correctly handles symbolic links, including not calling shutil.rmtree() on a symbolic link (which will pass the os.path.isdir() test if it links to a directory). It handles read-only files nicely.
Error: User Rate Limit Exceeded
11

Error: User Rate Limit Exceeded

os.system('rm '+test)

Error: User Rate Limit Exceeded

import glob, os
test = '/path/*'
r = glob.glob(test)
for i in r:
   os.remove(i)
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
1

Error: User Rate Limit Exceeded

def rmtree(dirname):
    retry = True
    while retry:
        retry = False
        try:
            shutil.rmtree(dirname)
        except exceptions.WindowsError, e:
            if e.winerror == 5: # No write permission
                win32api.SetFileAttributes(dirname, win32con.FILE_ATTRIBUTE_NORMAL)
                retry = True
3

Error: User Rate Limit Exceeded

os.popen('rm -f ./yourdir')
Error: User Rate Limit Exceededrm -f directoryError: User Rate Limit Exceeded
0

45

os.remove()Error: User Rate Limit Exceededos.rmdir()Error: User Rate Limit Exceeded

Error: User Rate Limit Exceededshutil.rmtree()Error: User Rate Limit Exceeded

import shutil
shutil.rmtree('/home/me/test') 

Error: User Rate Limit Exceededand the sub-directoriesError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded/home/me/test/*Error: User Rate Limit Exceededos.removeError: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
2

Похожие вопросы