Я разрабатываю код, предназначенный для организации папки «Загрузки». У меня есть код python, который уже перемещает нужные файлы в специальные папки.
Мой вопрос: как я могу использовать watchmedo для просмотра папок с загруженными файлами, и когда в него добавляется какой-либо файл, он запускает мой python код? Есть ли лучший способ сделать это?
Ниже я опубликую код, который я буду sh выполнять, когда новый файл будет найден в моей папке загрузок.
def on_modified(self, event):
for filename in os.listdir(downloads_folder):
#print(filename)
new_destination = type_check(filename)
# Add move date to the file name
new_name = str(datetime.now()).split()[0] + ' - ' + filename
# if we will move the file AND the file don't exists on the destiny folder
if new_destination and not os.path.isfile(new_destination + "/" + new_name):
# Set paths
src = downloads_folder + "/" + filename
new_destination += "/" + new_name
# Check if the file is downloading
is_downloading(src)
shutil.move(src, new_destination)
time.sleep(1)
# Sometimes we have a junk file on the old folder, so we will delete it, if it's there
try:
os.remove(src)
except:
continue
Заранее спасибо.