【问题标题】:How to automatically transfer Images to a Folder如何自动将图像传输到文件夹
【发布时间】:2020-12-25 14:16:55
【问题描述】:

我一直在尝试测试我的自动文件传输程序,但我想不出办法让它知道文件是否为“.png/.jpg”所以它可以自动将屏幕截图传输到文件夹,而不是从桌面。

我一直在尝试不同的方法来做到这一点,但是没有运气。请帮忙。

from watchdog.observers import Observer
import time
from watchdog.events import FileSystemEventHandler
import os
import json

class MyHandler(FileSystemEventHandler):

    def on_modified(self, event):
        for filename in os.listdir(folder_to_track):
            src = folder_to_track + "/" + filename
            new_destination = folder_destination + "/" + filename
            os.rename(src, new_destination)


folder_to_track = '/Users/mattbecute/Desktop'
folder_destination = '/Users/mattbecute/Desktop/Screenshots'
event_handler = MyHandler()
observer = Observer()
observer.schedule(event_handler, folder_to_track, recursive=True)
observer.start()

try:
    while True:
        time.sleep(10)
except KeyboardInterrupt:
    observer.stop()
observer.join(3)

【问题讨论】:

    标签: python-3.x macos file automation


    【解决方案1】:

    您可以使用glob 包仅列出以特定扩展名结尾的文件。

    import glob
    folder_to_track = '/Users/mattbecute/Desktop'
    im_files=glob.glob(folder_to_track+"/*.png")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-07
      • 2018-06-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多