简体   繁体   中英

How to handle the File hand-off from windows in a python program

I want to set up my python program to process all pdfs that are opened on my system, and then hand the processed pdf off to a standard reader.

So I register my program with windows as the default handler for .pdf files and windows presumably will run my program on the pdf file.

How within the script do I access this file. Is the file name one the sys.argvs ?

I didn't get google to work for me here.

So, yes, Windows passes the file name into the script as one of the sys.argvs. It is (so far as I can tell from printing the value) a file name without the path, that I used to open the file, so that tells me that Windows starts my program with the working directory set to the directory of the file it's called on.

One word of caution, a gotcha of sorts, registering my .py as the default handler, did not work -- clicking on the file, resulted in Windows complaining that the file was not a valid windows executable. I didn't do any research but turned my .py into an .exe (py2exe) and registered that as the default file handler which did work.

UPDATE, did not test it out but was told that specifying the python interpreter with my script as the default file handler would solve the "not valid" issue. Like this "C:\\Python2.7\\python.exe yourscript.py %*" the %* is to make so that the file name is made available to the script. (adding this without testing because on the one hand my problem has been solved and on the other what I was told makes sense to me)

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