Haris
Haris

Reputation: 85

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?

Upvotes: 3

Views: 3737

Answers (2)

MegaEmailman
MegaEmailman

Reputation: 545

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

Upvotes: 2

Denis Rurak
Denis Rurak

Reputation: 21

I think you need some kind of "listening" script running on background which will monitor files in download directory

Upvotes: 2

Related Questions