How can I make windows execute a python script when a flash drive is plugged into the computer? Is there a way to do this with python or do I have to use windows?

Portable Python installs a portable version of Python right on your USB flash drive (plan on 150MB for each version). You can use the flash drive now on Windows computers that do not have Python installed. It comes in Python25, Python26 and Python30 flavors. See:
http://www.portablepython.com/releases/

Portable Python installs a portable version of Python right on your USB flash drive (plan on 150MB for each version). You can use the flash drive now on Windows computers that do not have Python installed. It comes in Python25, Python26 and Python30 flavors. See:
http://www.portablepython.com/releases/

You misunderstood me. What I want is to run a python script /program that I have already written and is stored on the PC. Every time I plug the flash drive in , that script should be run automatically.

You misunderstood me. What I want is to run a python script /program that I have already written and is stored on the PC. Every time I plug the flash drive in , that script should be run automatically.

This would have to come from the Win API. You can look through the win32 extension module and see if something is there.

Time to quit, I am getting too old!

This would also likely require that your Python program run in the background as a service. It would need to monitor the presence of a USB flash drive and act accordingly when it detected one. There is information in this forum for setting up a service in Windows if you search for it.

are you planning to make a virus ? ;)

That was exactly my thought. It could be a virus extension to further spread itself to other directories.

are you planning to make a virus ? ;)

Lol.. no.. I'v already made a script to backup my course notes. I want a script that will automatically copy those backups (which are tarballs) to a flash drive the moment is it plugged in. (Eventually I want to run the copying script only when MY flash drive is plugged in.. but that can wait.)
(this may not be the most appropriate method.. but I want to give it a shot anyway)

If you make a script that do what you want, and then use py2exe.
It should be possible to to it like this.

[autorun]
open = Your_py2exe_file.exe
action = Run my program
icon = your.ico

Save as autorun.inf in your root usb drive.
You also have tool like PStart that you can use.
http://www.pegtop.de/start/

Thanks.. but I would like the program to be on my PC instead of on my flash drive...

The autorun.inf file has to be on your usb/cd/dvd drive.
Or nothing will start.

Your exe file you can have on your pc.
Just set path in your autorun.inf
Eksp.
open = c:/my_backup/backup.exe

Thanks.. I'll try that.

In that case. You have the following options.

1) Have a program constantly running and checking for the flash drive
2) Screw with the registry

The first one is what I suggest you do.

In that case. You have the following options.

1) Have a program constantly running and checking for the flash drive
2) Screw with the registry

The first one is what I suggest you do.

EDIT:

He edited before I posted my message :/ .

How would I have a python program running constantly?

Yes. Or you could use a 3rd party program to start the file when the flash drive is plugged in.

How would I have a python program running constantly?

If you are running windows you can just put your program in the 'startup' folder in your start menu and it will be started every time the computer turns on.

Though, if somehow the program quits, be that because of an error or some other means it will not be restarted.

Thanks.. I think I've found everything I need. However, just for the sake of curiosity, what would I do on linux? (I have a linux PC as well... )

um, here's what you do.
On your pc, make a file in your documents that is named something like:
runpython.pythonscript
It doesn't matter what it is, just so long as no one else will have it. Then, make a script that checks for that file in your documents, and if it's there, run the script on your flash drive
If not, then do nothing
Then, compile it using py2exe, and put it on your flash drive, along with the script, and the autorun.inf file.
That's how I'd do it

Thanks... Very usefull

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.