By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,035 Members | 1,324 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,035 IT Pros & Developers. It's quick & easy.

os.startfile and unknown file extension

P: 75
I need to open PDF file with my python application, and I'm using os.startfile(filename.pdf) command, I would need open command act 100% like clicking the file in the windows explorer

python documentation about os.startfile: "Start a file with its associated application. This acts like double-clicking the file in Windows Explorer, or giving the file name as an argument to the start command from the interactive command shell: the file is opened with whatever application (if any) its extension is associated."

well this isn't true! what happens when you doubleclick file with unknown file extension in Windows Explorer? popup appears saying that the file extension is unknown! and this isn't happening when using os.startfile()

does anyone have solution for this?

os.startfile() returns error "1155", No application is associated with the specified file for this operation

so should I check for this error message and then manually start that unknown file extension popup "application" ? not really a python question but does anyone know how to start that popup externally? =)
Apr 2 '08 #1
Share this Question
Share on Google+
2 Replies


P: 75
ok, I have almost solved the problem

Expand|Select|Wrap|Line Numbers
  1. try:
  2.    os.startfile("testfile.pdf")
  3. except Expection, errorcode:
  4.  
  5.    if errorcode[0] = 1155:
  6.       os.popen("Rundll32.exe SHELL32.dll, OpenAs_RunDLL testfile.pdf")
  7.    else:
  8.       print "other error"
  9.  
this works but as you know os.popen stops python code and not resume until I close it, I need to my code continue ofcourse and not stop for that popup =(

how to use os.startfile() with "Rundll32.exe SHELL32.dll, OpenAs_RunDLL testfile.pdf" command =/ it seems that os.startfile() take only filename and not any optional arguments

you can yourself try in windows using start->run... and see how the "Rundll32.exe SHELL32.dll, OpenAs_RunDLL testfile.pdf" acts :)
Apr 2 '08 #2

P: 75
instead of
Expand|Select|Wrap|Line Numbers
  1. os.popen("Rundll32.exe SHELL32.dll, OpenAs_RunDLL testfile.pdf")
  2.  
I tried
Expand|Select|Wrap|Line Numbers
  1. os.spawnl(os.P_NOWAIT, os.environ['WINDIR']+"/system32/Rundll32.exe", "Rundll32.exe SHELL32.DLL, OpenAs_RunDLL testfile.pdf")
  2.  
os.environ['WINDIR'] points to c:/windows or whatever the win installation directory is

it isn't pretty but it works, don't know why I have to give the name "Rundll32.exe" in the argument also, well it doesn't matter what there is ("a" works also) but if I dont give it, it won't work. maybe it's some linux thing =p

if someone knows cleaner solution, tell me =)
Apr 2 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.