英文:
Instantaneous file delete using Python
问题
我有一个脚本,它在一个循环中等待特定文件夹被生成,然后删除文件夹内文件名中包含 "_GOOD" 的图像(这些图像也在生成中)。当我在我的笔记本电脑上使用本地文件夹模拟运行该脚本时,它运行得很顺利。然而,当我在它打算使用的机器上运行这个脚本时,文件删除不是连续的。它会删除一个或两个图像,然后回到循环,尽管机器已经生成了新的图像。
请注意,这台机器生成图像非常快。它在捕获图像后立即生成它们。这可能是问题吗?我的脚本无法理解机器图像生成的速度吗?或者我的代码有问题吗?感谢您的意见!
以下是代码:
reel_folders_src = \\directory_of_folders_that_are_yet_to_exist\\
def wait_for_reelfolders(folder_paths):
last_modified_folder = {folder_path3: None for folder_path3 in folder_paths}
while True:
print("Waiting for other reel folders")
for folder_path3 in folder_paths:
if os.path.exists(folder_path3):
current_modified = os.path.getmtime(folder_path3)
if last_modified_folder[folder_path3] is None or current_modified > last_modified_folder[folder_path3]:
last_modified_folder[folder_path3] = current_modified
#delete_files_in_folder(folder_path3)
for file_name in os.listdir(folder_path3):
file_path = os.path.join(folder_path3, file_name)
print(f"Ongoing GOOD image purge in folder '{folder_path3}'")
if "_GOOD_" in file_path:
os.remove(file_path)
#这些是机器尚未生成的文件夹,其中包含图像
reel_folders = [reel_folders_src + '\\' + "Reel2", reel_folders_src + '\\' + "Reel3", reel_folders_src + '\\' + "Reel4"]
#执行函数
wait_for_reelfolders(reel_folders)
英文:
I have a script that is on a loop that waits for certain folders to be generated and then deletes the images inside of them with "_GOOD" on their filename (the images are also being generated). The script runs smoothly when simulated on my laptop using my local folders. However, when I run this script on the machine it is intended to be used, the file deletion is not continuous. It will delete one or two images then goes back to the loop even though there are new images generated by the machine.
Note that this machine generates images very fast. It generates the images as soon as it captures them. Could this be the problem? My script cannot comprehend the speed of image generation of the machine? Or is there something wrong with my code? Appreciate the inputs!
Below is the code:
reel_folders_src = \\directory_of_folders_that_are_yet_to_exist\\
def wait_for_reelfolders(folder_paths):
last_modified_folder = {folder_path3: None for folder_path3 in folder_paths}
while True:
print("Waiting for other reel folders")
for folder_path3 in folder_paths:
if os.path.exists(folder_path3):
current_modified = os.path.getmtime(folder_path3)
if last_modified_folder[folder_path3] is None or current_modified > last_modified_folder[folder_path3]:
last_modified_folder[folder_path3] = current_modified
#delete_files_in_folder(folder_path3)
for file_name in os.listdir(folder_path3):
file_path = os.path.join(folder_path3, file_name)
print(f"Ongoing GOOD image purge in folder '{folder_path3}'")
if "_GOOD_" in file_path:
os.remove(file_path)
#These are the folders that are yet to be generated by the machine along with images inside of them
reel_folders = [reel_folders_src + '\\' + "Reel2", reel_folders_src + '\\' + "Reel3", reel_folders_src + '\\' + "Reel4"]
#Execute function
wait_for_reelfolders(reel_folders)
答案1
得分: 1
以下是翻译好的部分:
你有一些创建文件的应用程序在一个目录中生成文件。这些文件的路径中可能包含"GOOD"。
问题中的代码正在轮询目录列表,以查看它们是否已被修改(自上次检查以来 - 如果有的话)。如果目录似乎已被修改,那么该目录中包含"GOOD"的文件将被删除。
然而,需要注意的是,当这段代码正在删除文件时,其他应用程序可能正在创建文件!因此,os.listdir()的输出只是一个快照,符合该模式的文件可能会被忽略。
另外值得一提的是,当运行wait_for_reelfolders()函数时,硬件可能会不断切换,因为while循环是无限的,而且没有实现延迟。
这是我的建议:
import time
import os
import glob
import threading
EVENT = threading.Event()
reel_folders_src = r'c:\Reels'
def wait_for_reel_folders(*folder_paths):
while not EVENT.is_set():
for folder in folder_paths:
for good in glob.glob(os.path.join(folder, '*_GOOD_*')):
os.remove(good)
time.sleep(1) # 让计算机休息一下
reel_folders = [os.path.join(reel_folders_src, f'Reel{r}') for r in range(2, 5)]
(thread := threading.Thread(target=wait_for_reel_folders, args=reel_folders)).start()
time.sleep(10) # 运行线程10秒钟
EVENT.set() # 告诉线程停止
thread.join() # 等待线程终止
这将不断监视folder_paths列表中列出的目录,直到控制应用程序通过设置Event来告诉它停止。
glob()的一个方便功能是,如果搜索的模式不存在,它会简单地返回一个空列表 - 即没有异常。这意味着尚不存在的目录会被忽略。
英文:
You have some application that is creating files in a directory. Such files may have _GOOD_ in their pathname.
The code in the question is polling a list of directories to see if they've been modified (since they were last checked - if ever). If the directory appears to have been modified, files containing _GOOD_ within that directory will be deleted.
HOWEVER While this code is deleting files, the other application may be creating them! Therefore the output from os.listdir() is merely a snapshot and files matching the pattern may be missed.
As an aside, it's worth noting that your hardware will "thrash" when you run the wait_for_reelfolders() function because the while loop is infinite and there are no delays implemented
Here's what I would suggest:
import time
import os
import glob
import threading
EVENT = threading.Event()
reel_folders_src = r'c:\Reels'
def wait_for_reel_folders(*folder_paths):
while not EVENT.is_set():
for folder in folder_paths:
for good in glob.glob(os.path.join(folder, '*_GOOD_*')):
os.remove(good)
time.sleep(1) # give the computer a break
reel_folders = [os.path.join(reel_folders_src, f'Reel{r}') for r in range(2, 5)]
(thread := threading.Thread(target=wait_for_reel_folders, args=reel_folders)).start()
time.sleep(10) # run thread for 10 seconds
EVENT.set() # tells thread to stop
thread.join() # wait for thread to terminate
This will continually monitor the folders listed in folder_paths until such time as the controlling application tells it to stop by setting the Event.
One convenient feature of glob() is that if the pattern being searched for doesn't exist it simply returns an empty list - i.e., no exception. This means that the folders that may not yet exist are ignored
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论