简体   繁体   中英

How to make python program run automatically every time I download a file?

I want to create a program that runs automatically every time I download a file from browser.

For example, when I download an image file from chrome, the program runs automatically and perform tasks. Is it possible?

You could have a script using len(os.listdir()) to see how many files are in your downloads folder, and anytime that number changes, perform your desired operation on the newest file

EDIT:

You'd want a Python script looking something like this:

import time
import os
While True:
    NumberOfFiles=len(os.listdir("C:\Path\To\Downloads\Folder"))
    time.sleep(20)
    OldNumber = NumberOfFiles
    NumberOfFiles = len(os.listdir("C:\Path\To\Downloads\Folder"))
    if NumberOfFiles != OldNumber:
        #This is where you put the lines of code you want executed when you download something.

And then you'd need a .bat file with something like

C:\Path\To\Python C:\Path\To\That\Script

You could put that .bat file in your Startup folder, or use the Task Scheduler utility to make it start running everytime you log on to your computer.

Hope this helps! At the moment it checks for new files only once every 20 seconds. You could make that check more often by lowering the number there, but the more often it checks, the more resources this is gonna use. If you don't download things to often, I'd even suggest changing it to something like 60

我认为你需要在后台运行某种“监听”脚本,它将监视下载目录中的文件

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM