[英]How to remove all empty files within folder and its sub folders?
I am trying to remove all empty files in a folder, and there are folders within the folder so it needs to check inside those folders too:我正在尝试删除文件夹中的所有空文件,并且文件夹中有文件夹,因此它也需要检查这些文件夹中的内容:
eg remove all empty files within C:\\folder1\\folder1 and C:\\folder1\\folder2 etc例如删除 C:\\folder1\\folder1 和 C:\\folder1\\folder2 等中的所有空文件
import sys
import os
def main():
getemptyfiles(sys.argv[1])
def getemptyfiles(rootdir):
for root, dirs, files in os.walk(rootdir):
for d in ['RECYCLER', 'RECYCLED']:
if d in dirs:
dirs.remove(d)
for f in files:
fullname = os.path.join(root, f)
try:
if os.path.getsize(fullname) == 0:
print fullname
os.remove(fullname)
except WindowsError:
continue
This will work with a bit of adjusting:这将需要一些调整:
The os.remove()
statement could fail so you might want to wrap it with try...except
as well. os.remove()
语句可能会失败,因此您可能想用try...except
包装它。 WindowsError
is platform specific. WindowsError
是特定于平台的。 Filtering the traversed directories is not strictly necessary but helpful.过滤遍历的目录不是绝对必要的,但很有帮助。
The for loop uses dir to find all files, but not directories, in the current directory and all subfolders recursively. for 循环使用 dir 递归查找当前目录和所有子文件夹中的所有文件,但不查找目录。 Then the second line checks to see if the length of each file is less than 1 byte before deleting it.
然后第二行在删除之前检查每个文件的长度是否小于 1 个字节。
cd /d C:\folder1
for /F "usebackq" %%A in (`dir/b/s/a-d`) do (
if %%~zA LSS 1 del %%A
)
import os
while(True):
path = input("Enter the path")
if(os.path.isdir(path)):
break
else:
print("Entered path is wrong!")
for root,dirs,files in os.walk(path):
for name in files:
filename = os.path.join(root,name)
if os.stat(filename).st_size == 0:
print(" Removing ",filename)
os.remove(filename)
I do first remove empty files, afterwards by following this answer ( https://stackoverflow.com/a/6215421/2402577 ), I have removed the empty folders.我首先删除空文件,然后按照这个答案( https://stackoverflow.com/a/6215421/2402577 ),我删除了空文件夹。
In addition, I added topdown=False
in os.walk()
to walk from leaf to roo since the default behavior of os.walk()
is to walk from root to leaf.此外,我在
os.walk()
添加了topdown=False
以从叶子走到 roo,因为os.walk()
的默认行为是从根走到叶子。
So empty folders that also contains empty folders or files are removed as well.因此,还包含空文件夹或文件的空文件夹也将被删除。
import os
def remove_empty_files_and_folders(dir_path) -> None:
for root, dirnames, files in os.walk(dir_path, topdown=False):
for f in files:
full_name = os.path.join(root, f)
if os.path.getsize(full_name) == 0:
os.remove(full_name)
for dirname in dirnames:
full_path = os.path.realpath(os.path.join(root, dirname))
if not os.listdir(full_path):
os.rmdir(full_path)
I hope this can help you我希望这可以帮助你
#encoding = utf-8
import os
docName = []
def listDoc(path):
docList = os.listdir(path)
for doc in docList:
docPath = os.path.join(path,doc)
if os.path.isfile(docPath):
if os.path.getsize(docPath)==o:
os.remove(docPath)
if os.path.isdir(docPath):
listDoc(docPath)
listDoc(r'C:\folder1')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.