如何在Python中删除本地文件夹的内容?
目前的项目适用于Windows,但我也希望看到* nix。
更新为仅删除文件并使用评论中建议的os.path.join()
方法。如果您还想删除子目录,请取消注释elif
语句。
import os, shutil
folder = '/path/to/folder'
for the_file in os.listdir(folder):
file_path = os.path.join(folder, the_file)
try:
if os.path.isfile(file_path):
os.unlink(file_path)
#elif os.path.isdir(file_path): shutil.rmtree(file_path)
except Exception as e:
print(e)
你可能最好使用os.walk()
。
os.listdir()
不区分文件和目录,你很快就会试图取消链接这些文件。有一个很好的例子,使用os.walk()
递归删除目录here,并提示如何使其适应您的环境。
我曾经这样解决问题:
import shutil
import os
shutil.rmtree(dirpath)
os.mkdir(dirpath)
我知道这是一个老线程,但我从python的官方网站上发现了一些有趣的东西。仅仅是为了分享另一个删除目录中所有内容的想法。因为在使用shutil.rmtree()时我有一些授权问题,我不想删除目录并重新创建它。原地址是http://docs.python.org/2/library/os.html#os.walk。希望可以帮助别人。
def emptydir(top):
if(top == '/' or top == "\\"): return
else:
for root, dirs, files in os.walk(top, topdown=False):
for name in files:
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))
又一个解决方案:
import sh
sh.rm(sh.glob('/path/to/folder/*'))
如果您使用的是* nix系统,为什么不利用系统命令?
import os
path = 'folder/to/clean'
os.system('rm -rf %s/*' % path)
我很惊讶没有人提到这个令人敬畏的pathlib
做这项工作。
如果您只想删除目录中的文件,它可以是oneliner
from pathlib import Path
[f.unlink() for f in Path("/path/to/folder").glob("*") if f.is_file()]
要递归删除目录,您可以编写如下内容:
from pathlib import Path
from shutil import rmtree
for path in Path("/path/to/folder").glob("**/*"):
if path.is_file():
path.unlink()
elif path.is_dir():
rmtree(path)
我通过在以下之间添加rmtree
解决了makedirs
time.sleep()
的问题:
if os.path.isdir(folder_location):
shutil.rmtree(folder_location)
time.sleep(.5)
os.makedirs(folder_location, 0o777)
针对有限的特定情况的答案:假设您要在维护子文件夹树的同时删除文件,您可以使用递归算法:
import os
def recursively_remove_files(f):
if os.path.isfile(f):
os.unlink(f)
elif os.path.isdir(f):
map(recursively_remove_files, [os.path.join(f,fi) for fi in os.listdir(f)])
recursively_remove_files(my_directory)
也许稍微偏离主题,但我认为很多人会发现它很有用
假设要删除temp_dir
,使用os
的单行命令将是:
_ = [os.remove(os.path.join(save_dir,i)) for i in os.listdir(temp_dir)]
注意:这只是用于删除文件的1行 - “不删除目录”。
希望这可以帮助。谢谢。
使用下面的方法删除目录的内容,而不是目录本身:
import os
import shutil
def remove_contents(path):
for c in os.listdir(path):
full_path = os.path.join(path, c)
if os.path.isfile(full_path):
os.remove(full_path)
else:
shutil.rmtree(full_path)
试试shutil模块
import shutil
shutil.rmtree('/path/to/folder')
说明:
shutil.rmtree(path, ignore_errors=False, onerror=None)
Docstring:递归删除目录树。
如果设置了
ignore_errors
,则忽略错误;否则,如果设置了onerror
,则调用(func, path, exc_info)
来处理错误,其中func
是os.listdir
,os.remove
或os.rmdir
; path是导致它失败的那个函数的参数;和exc_info
是由sys.exc_info()
归来的元组。如果ignore_errors
为假而onerror
为None
,则会引发异常。
重要说明:请注意shutil.rmtree()
不仅删除目标文件夹的内容。它也会删除文件夹本身。
只需这样做。这将删除目录内的所有文件以及子目录中的所有文件。不伤害任何文件夹/目录。在Ubuntu上运行良好,没有任何错误。
import os
mypath = "my_folder" #Enter your path here
for root, dirs, files in os.walk(mypath):
for file in files:
os.remove(os.path.join(root, file))
这应该只是使用OS模块列出然后删除!
import os
DIR = os.list('Folder')
for i in range(len(DIR)):
os.remove('Folder'+chr(92)+i)
为我工作,任何问题让我知道!
你可以这样做:
import os
import glob
files = glob.glob('/YOUR/PATH/*')
for f in files:
os.remove(f)
您当然可以在路径中使用其他过滤器,例如:/YOU/PATH/*.txt用于删除目录中的所有文本文件。
扩展mhawke的答案这是我实施的。它会删除文件夹的所有内容,但不会删除文件夹本身。在Linux上使用文件,文件夹和符号链接进行测试,也可以在Windows上运行。
import os
import shutil
for root, dirs, files in os.walk('/path/to/folder'):
for f in files:
os.unlink(os.path.join(root, f))
for d in dirs:
shutil.rmtree(os.path.join(root, d))
使用rmtree
并重新创建文件夹可能会有效,但我在删除并立即重新创建网络驱动器上的文件夹时遇到了错误。
使用walk的建议解决方案不起作用,因为它使用rmtree
删除文件夹,然后可能尝试在以前在这些文件夹中的文件上使用os.unlink
。这会导致错误。
发布的glob
解决方案还将尝试删除非空文件夹,从而导致错误。
我建议你使用:
folder_path = '/path/to/folder'
for file_object in os.listdir(folder_path):
file_object_path = os.path.join(folder_path, file_object)
if os.path.isfile(file_object_path):
os.unlink(file_object_path)
else:
shutil.rmtree(file_object_path)
这是迄今为止唯一的答案,其中:
码:
for filename in os.listdir(dirpath):
filepath = os.path.join(dirpath, filename)
try:
shutil.rmtree(filepath)
except OSError:
os.remove(filepath)
与许多其他答案一样,这不会尝试调整权限以启用文件/目录的删除。
作为oneliner:
import os
# Python 2.7
map( os.unlink, (os.path.join( mydir,f) for f in os.listdir(mydir)) )
# Python 3+
list( map( os.unlink, (os.path.join( mydir,f) for f in os.listdir(mydir)) ) )
考虑文件和目录的更强大的解决方案也是(2.7):
def rm(f):
if os.path.isdir(f): return os.rmdir(f)
if os.path.isfile(f): return os.unlink(f)
raise TypeError, 'must be either file or directory'
map( rm, (os.path.join( mydir,f) for f in os.listdir(mydir)) )
注意:如果有人拒绝投票给我答案,我在这里有一些解释。
shutil.rmtree()
可以用来删除目录树。我在自己的项目中多次使用它。但是你必须意识到shutil.rmtree()
也会删除目录本身。虽然这对某些人来说可能是可以接受的,但它不是删除文件夹内容的有效答案(没有副作用)。shutil.rmtree()
删除它并用os.mkdir()
重建它。并且您将获得一个带有默认(继承)所有者和模式位的空目录。虽然您可能有权删除内容甚至目录,但您可能无法在目录上设置原始所有者和模式位(例如,您不是超级用户)。这是一个漫长而丑陋但可靠而有效的解决方案。
它解决了其他问题无法解决的一些问题:
shutil.rmtree()
(如果它链接到目录,将通过os.path.isdir()
测试;甚至os.walk()
的结果也包含符号链接目录)。这是代码(唯一有用的功能是clear_dir()
):
import os
import stat
import shutil
# http://stackoverflow.com/questions/1889597/deleting-directory-in-python
def _remove_readonly(fn, path_, excinfo):
# Handle read-only files and directories
if fn is os.rmdir:
os.chmod(path_, stat.S_IWRITE)
os.rmdir(path_)
elif fn is os.remove:
os.lchmod(path_, stat.S_IWRITE)
os.remove(path_)
def force_remove_file_or_symlink(path_):
try:
os.remove(path_)
except OSError:
os.lchmod(path_, stat.S_IWRITE)
os.remove(path_)
# Code from shutil.rmtree()
def is_regular_dir(path_):
try:
mode = os.lstat(path_).st_mode
except os.error:
mode = 0
return stat.S_ISDIR(mode)
def clear_dir(path_):
if is_regular_dir(path_):
# Given path is a directory, clear its content
for name in os.listdir(path_):
fullpath = os.path.join(path_, name)
if is_regular_dir(fullpath):
shutil.rmtree(fullpath, onerror=_remove_readonly)
else:
force_remove_file_or_symlink(fullpath)
else:
# Given path is a file or a symlink.
# Raise an exception here to avoid accidentally clearing the content
# of a symbolic linked directory.
raise OSError("Cannot call clear_dir() on a symbolic link")
import os
import shutil
# Gather directory contents
contents = [os.path.join(target_dir, i) for i in os.listdir(target_dir)]
# Iterate and remove each item in the appropriate manner
[os.remove(i) if os.path.isfile(i) or os.path.islink(i) else shutil.rmtree(i) for i in contents]
之前的评论还提到在Python 3.5+中使用os.scandir。例如:
import os
import shutil
with os.scandir(target_dir) as entries:
for entry in entries:
if entry.is_file() or entry.is_symlink():
os.remove(entry.path)
elif entry.is_dir():
shutil.rmtree(entry.path)