[Tutor] os.startfile

Lang Hurst lang at tharin.com
Mon Dec 20 00:50:50 CET 2010


On 12/19/2010 01:16 PM, Steven D'Aprano wrote:
> Lang Hurst wrote:
>> I have the following in my program:
>>
>>
>>         try:
>>             os.startfile('current_credit.txt')
>>         except:
>>             os.system('/usr/bin/xdg-open current_credit.txt')
>>
>>
>> Basically, open a file in notepad if I'm on windows, vim if on my 
>> home linux computer.  It works fine in linux and in Windows through 
>> virtualbox.  The problem is when I take the program to work, it 
>> doesn't open the file.  The computers at work are pretty locked down, 
>> so I'm thinking it has something to do with the os.startfile 
>> command.  Are there any alternative commands I could try?
>
> You don't give us much information to go on, such as the version of 
> Python you use, or the operating system on your work desktops, or the 
> error that you see when you try this, or even if you can open the file 
> by double-clicking it, but that's okay, because I love guessing games!
>
> I guess that the problem is that your work desktops are, in fact, 
> Apple Macintoshes running OS-X. Am I close?
>
> Other than that, you shouldn't just blindly ignore the exception 
> raised by startfile. Not all exceptions mean "You're not running 
> Windows and there is no startfile command", and you shouldn't catch 
> bare excepts. You would be better off doing this:
>
> try:
>     os.startfile('current_credit.txt')
> except AttributeError:
>     # No startfile command, so we're not on Windows.
>     # Try a Linux command instead.
>     # (Tested on Fedora, may not work on all distros.)
>     os.system('/usr/bin/xdg-open current_credit.txt')
>
>
> That way, when you get a different error, like "file not found" or 
> "permission denied", you will see what it is, and perhaps get a hint 
> as to what the problem is.
>
> Python doesn't have super powers. If you can't open a file because the 
> desktop has been locked down, then Python won't be able to magically 
> open it. It has no more permissions to do things than you do. There's 
> no magic command "open files even if I'm not allowed to open them".
>
>

Sorry for the lack of information.  I'm using Python 2.6.6, glade, gtk, 
vim.  Once the program does what I want, I boot up the virtualbox image 
(XP) and try it in there.  Usually it doesn't have a problem.  If all 
works well, I wrap it all up into an executable using pyinstaller.  Then 
I try to run the exe on XP.  That works, so I pull it back into linux 
(Debian Sid, for what that's worth) and run the executable via wine.  
Everything checks out.

I can't install anything at work (XP computer), hence the stand alone 
file.  Then when I run it, everything works fine, except when I get to 
the point where I want notepad to open the file.  I can browse to the 
file and manually open it with notepad and it's fine.  It just won't 
open with notepad from the script.

I just don't really know Windows that well.  I was just wondering if my 
work computer being locked down like it is would stop the os.startfile 
command, and if so, do I have any alternative ways to do what I'm trying 
to do (open a text file).

-- 
There are no stupid questions, just stupid people.



More information about the Tutor mailing list