首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >第9章,使用Python实践项目自动化无聊的内容:删除不需要的文件

问第9章,使用Python实践项目自动化无聊的内容:删除不需要的文件
EN

Stack Overflow用户
提问于 2017-09-17 20:22:06
回答 2查看 1.4K关注 0票数 0

我正在尝试第9章中的第二个练习项目,即用Python自动化无聊的东西,但是即使它输出了预期的字符串,它也不执行os.unlink(filename)命令,文件没有被删除,它们仍然完好无损。有人能帮忙吗?下面是我使用的代码:

代码语言:javascript
复制
#! 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')
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-09-17 20:24:31

这个代码就是问题所在:

代码语言:javascript
复制
if int(fileSize) < 100000000:
    continue
    os.unlink(filename)

在调用os.unlink之前,有一个continue语句,它跳到循环的下一个迭代。

我想你是想让os.unlink在那个条件之外。只是撤销它:

代码语言:javascript
复制
if int(fileSize) < 100000000:
    # skip small files
    continue
# Otherwise, delete the file
os.unlink(filename)

更新

正如上面的注释所指出的,您还需要构建一个完整的路径:

代码语言:javascript
复制
os.unlink(os.path.join(foldername, filename))

更新2

而不是if ...: continue,您可以反转条件的逻辑来简化代码。下面是我清理过的代码版本:

代码语言:javascript
复制
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生成的组件组成。
  • 我重命名了一些变量以符合os.walk文档和Python风格(snake_case而不是camelCase)。
  • 我使用的是str.format而不是字符串连接。
票数 2
EN

Stack Overflow用户

发布于 2018-09-28 17:03:39

这就是我解决这个问题的方法,因为这个问题只要求列出大于100 we的文件,我们可以跳过删除部分。

代码语言:javascript
复制
#! 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.')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46268521

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档