aiofiles - 根据特定条件异步删除文件夹中的文件
aiofiles - Delete files in folder asynchronously based on certain criteria
我有一个包含大约 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())
我假设您正在使用 python >= 3.5,您正在使用 aiofiles.open
作为上下文管理器,因此您不必担心自己关闭文件。您需要做的只是简单地退出上下文管理器块,当您的条件确定应该删除文件时,然后在上下文管理器块之后删除文件(是的,os.remove
是正确的函数作业,只需确保您不需要绝对路径即可)。
遗憾的是,您不能将 break
与上下文管理器一起使用,但 this question 显示了实现相同结果的各种方法。
我有一个包含大约 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())
我假设您正在使用 python >= 3.5,您正在使用 aiofiles.open
作为上下文管理器,因此您不必担心自己关闭文件。您需要做的只是简单地退出上下文管理器块,当您的条件确定应该删除文件时,然后在上下文管理器块之后删除文件(是的,os.remove
是正确的函数作业,只需确保您不需要绝对路径即可)。
遗憾的是,您不能将 break
与上下文管理器一起使用,但 this question 显示了实现相同结果的各种方法。