【问题标题】:aiofiles - Delete files in folder asynchronously based on certain criteriaaiofiles - 根据特定条件异步删除文件夹中的文件
【发布时间】:2021-04-05 14:59:21
【问题描述】:

我有大约 50 000 个 HTML 文件的文件夹。 我正在尝试编写打开文件的脚本,如果标题包含某些字符串,则应该删除文件。

这是我目前的尝试:

import aiofiles
import glob
from natsort import natsorted
import asyncio
from bs4 import BeautifulSoup
import os

    async def main():
        i=0
        htmls = glob.glob("CarsPages" + "//*.html")
        for html in natsorted(htmls):
            async with aiofiles.open(html, mode='r', encoding='UTF-8',  errors='strict', buffering=1) as f:
                contents = await f.read()
                soup = BeautifulSoup(contents, features="lxml")
                if "Best portal" in soup.title.get_text():
                    i+=1
                    os.close(html) 
                    os.remove(html)
                    print("removing: ", html)
        print("Removed: ", i, " pages")
    
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())

但我得到了:

os.close(html) TypeError: an integer is required (got type str)

不知道用 aiofiles 打开后要使用哪些函数来关闭和删除?

编辑 - 基于@joao 回答的工作代码

import aiofiles
import glob
from natsort import natsorted
import asyncio
from bs4 import BeautifulSoup
import os

async def main():
    i=0
    htmls = glob.glob("CarsPages" + "//*.html")
    for html in natsorted(htmls):
        async with aiofiles.open(html, mode='r', encoding='UTF-8',  errors='strict', buffering=1) as f:
            contents = await f.read()
            soup = BeautifulSoup(contents, features="lxml")
        if "Best portal" in soup.title.get_text():
            i+=1
            os.remove(html)
            print("removed: ", html)
    print("Removed: ", i, " pages")

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

【问题讨论】:

  • os.close() 接受文件描述符,试试os.close(f)
  • @KrishnaChaurasia 也尝试过,但得到了:os.close(f) TypeError: an integer is required (got type AsyncTextIOWrapper)

标签: python python-asyncio python-aiofiles


【解决方案1】:

我假设您使用 python >= 3.5,您使用 aiofiles.open 作为上下文管理器,因此您不必担心自己关闭文件。您需要做的只是退出上下文管理器块,当您的条件确定应该删除文件时,然后在上下文管理器块之后删除文件(是的,os.remove 是该工作的正确功能,只需确保您不需要绝对路径)。

很遗憾,您不能将break 与上下文管理器一起使用,但this question 显示了实现相同结果的各种方法。

【讨论】:

  • 效果很好,我已经编辑了我的问题以显示最终的工作代码。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-21
  • 1970-01-01
  • 2013-03-20
  • 1970-01-01
  • 1970-01-01
  • 2020-07-24
相关资源
最近更新 更多