第9章,用Python实践项目自动化枯燥的工作:删除不需要的文件
我正在尝试第9章中的第二个实践项目,即用Python自动化枯燥的东西,但即使它打印出预期的字符串,它也不会执行os.unlink(filename)命令,文件不会被删除,它们仍然保持不变。有人能帮忙吗?以下是我使用的代码:第9章,用Python实践项目自动化枯燥的工作:删除不需要的文件,python,Python,我正在尝试第9章中的第二个实践项目,即用Python自动化枯燥的东西,但即使它打印出预期的字符串,它也不会执行os.unlink(filename)命令,文件不会被删除,它们仍然保持不变。有人能帮忙吗?以下是我使用的代码: #! python3 # deleteUnneeded.py - Searches and deletes files and folders # to free up space on a computer import os # Define a function
#! python3
# deleteUnneeded.py - Searches and deletes files and folders
# to free up space on a computer
import os
# Define a function
def delUnneeded(folder):
folder = os.path.abspath(folder)
# Walk the folder tree with os.walk()
# and search for files and folders of more than 100MB using os.path.getsize()
for foldername, subfolders, filenames in os.walk(folder):
for filename in filenames:
fileSize = os.path.getsize(foldername + '\\' + filename)
if int(fileSize) < 100000000:
continue
os.unlink(filename)
print('Deleting ' + filename + '...')
delUnneeded('C:\\Users\\DELL\\Desktop\\flash')
print('Done')
#!蟒蛇3
#deleteUnneeded.py-搜索和删除文件和文件夹
#释放计算机上的空间
导入操作系统
#定义函数
def DELUNNEEED(文件夹):
folder=os.path.abspath(文件夹)
#使用os.Walk()遍历文件夹树
#并使用os.path.getsize()搜索超过100MB的文件和文件夹
对于os.walk(文件夹)中的foldername、子文件夹和文件名:
对于文件名中的文件名:
fileSize=os.path.getsize(foldername+'\\'+文件名)
如果int(文件大小)<100000000:
持续
取消链接(文件名)
打印('删除'+filename+'…'))
delUnneeded('C:\\Users\\DELL\\Desktop\\flash')
打印(‘完成’)
此代码就是问题所在:
if int(fileSize) < 100000000:
continue
os.unlink(filename)
更新
如上所述,您还需要构建完整路径:
os.unlink(os.path.join(foldername, filename))
更新2
您可以反转条件逻辑以简化代码,而不是if…:continue
。以下是我的代码清理版本:
import os
def del_unneeded(folder):
# Walk the folder tree with os.walk() and search for files of more than
# 100MB using os.path.getsize()
for dirpath, dirnames, filenames in os.walk(folder):
for filename in filenames:
full_path = os.path.join(dirpath, filename)
if os.path.getsize(full_path) > 100000000:
print('Deleting {}...'.format(full_path))
os.unlink(full_path)
del_unneeded('C:\\Users\\DELL\\Desktop\\flash')
print("Done")
其他小改动:
- 我删除了
,因为int(…)
已经返回了os.path.getsize
int
- 我使用了完整的路径,由
生成的组件组成os.walk
- 我重命名了一些变量以符合
文档和Python编码风格(os.walk
而不是snake\u case
)camelCase
- 我使用了
而不是字符串连接str.format
- 我就是这样解决这个问题的,因为这个问题只要求列出大于100MB的文件,所以我们可以跳过删除部分
#! python3
# delete_files.py - Don't get misled by the program name, hah. This program
# lists the files in a folder tree larger than 100MB and prints them to the screen.
import os
folder = os.path.abspath(input('Please enter folder to search for files larger than 100MB:\n'))
if os.path.isdir(folder) is True:
i = 0 # Placeholder for number of files found larger than 100MB
for foldername, subfolders, filenames in os.walk(folder):
for file in filenames:
abs_path = os.path.abspath(foldername)
full_path = os.path.join(foldername, file)
file_size = os.path.getsize(full_path)
if file_size > 100000000:
i += 1
print(full_path)
print(f'{i} files found larger than 100MB')
else:
print('Folder does not exist.')
在
os.unlink(filename)
之前有continue
语句,您可能还需要指定删除文件的完整路径
#! python3
# delete_files.py - Don't get misled by the program name, hah. This program
# lists the files in a folder tree larger than 100MB and prints them to the screen.
import os
folder = os.path.abspath(input('Please enter folder to search for files larger than 100MB:\n'))
if os.path.isdir(folder) is True:
i = 0 # Placeholder for number of files found larger than 100MB
for foldername, subfolders, filenames in os.walk(folder):
for file in filenames:
abs_path = os.path.abspath(foldername)
full_path = os.path.join(foldername, file)
file_size = os.path.getsize(full_path)
if file_size > 100000000:
i += 1
print(full_path)
print(f'{i} files found larger than 100MB')
else:
print('Folder does not exist.')