Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Os.startfile in Windows.
#1
I'd like to test this but I don't have a windows computer at home (the first thing I do when I purchase a new computer is erase the Windows OS), so can someone tell me what happens when you call os.startfile on a python program
import os
os.startfile("spam.py")
Does this open an editor with spam.py ready for editing, as I'm expecting it does or does it run spam.py or does it depend on the windows configuration?

I'm trying to obtain something similar to calling xdg-open spam.py in linux.
Reply
#2
Traceback (most recent call last):
  File "D:/test/test.py", line 2, in <module>
    os.startfile("spam.py")
WindowsError: [Error 2] The system cannot find the file specified: 'spam.py'
Reply
#3
fishhook Wrote:The system cannot find the file specified: 'spam.py'
Sorry I forgot to mention that I only need the answer when a file named 'spam.py' already exists and contains python code.
Reply
#4
Notepad++ was opened with 'spam.py' content in new tab.
Reply
#5
Look at webbrowser module.
webbrowser.open(filename) it can call os.startfile(), open, xdg-open where appropriate.

Source code
Unwillingly supported Snooty
Quote:Note that on some platforms, trying to open a filename using this function,may work and start the operating system’s associated program.
However, this is neither supported nor portable.

Test:
import webbrowser

webbrowser.open('Alarm.wav')
import os

os.startfile('Alarm.wav')
Both work the same and open program that file association with .wav in Windows.

This will not work as can not find file association for .wav.
import subprocess

subprocess.run(['Alarm.wav']) 
Discussion about making os.startfile work cross platform,don't think they agree on something.
Reply
#6
snippsat Wrote:webbrowser.open(filename) it can call os.startfile(), open, xdg-open where appropriate.
I think I'll keep calling these functions myself because in the webbrowser module there is a _tryorder list of preferred applications to use to start opening a program. In my linux system, xdg-open should be the first on the list but because I have defined a BROWSER environment variable, the webbrowser module changes the order and it inserts the BROWSER before xdg-open in the list. The result is that a python file opens in my web browser instead of the editor!

Looking at the code, it should fail to call os.startfile() in windows too if you define a BROWSER environment variable.

A solution could be to define several alternatives in BROWSER, separated by os.pathsep. For example I could insert xdg-open at the beginning.
Reply
#7
import os
os.startfile(<absolute path of my python file>)
When I ran above one, it is actually running and it is not allowing to edit the code.
Tried in Windows machine
Reply
#8
Malt Wrote:it is actually running and it is not allowing to edit the code.
This is in contradiction with fishhook's reply above. Now the problem is to understand what's going on. Does it do the same if you do
import webbrowser
webbrowser.open(<absolute path to your python file>)
?

Also what happens if you call
import os
os.startfile("spam.py", "edit")
?
Reply
#9
Gribouilli Wrote:
Malt Wrote:it is actually running and it is not allowing to edit the code.
This is in contradiction with fishhook's reply above. Now the problem is to understand what's going on. Does it do the same if you do
import webbrowser
webbrowser.open(<absolute path to your python file>)
It will all depend on what file association .py is set to in Windows.
If i set file association in Windows for .py to Notepad++,then this code will open hello.py in Notepad++.
import webbrowser

webbrowser.open(r'E:\div_code\hello.py')
If i chance back to root\python.exe will try to run hello.py.


Gribouillis Wrote:Also what happens if you call
import os

os.startfile("spam.py", "edit")
edit is removed from most newer Windows version.
It will all fail with .py.
import os

# Work with know print formates  format like txt,pdf,images... 
os.startfile("test.txt", "print")

# Fail
os.startfile("hello.py", "print")
Reply
#10
So does it mean that there is no safe way to launch an editor for a python file in windows without knowing the editor?
Or perhaps I can do
subprocess.call([sys.executable, '-m', 'idlelib', 'spam.py'])
but tkinter needs to be available. Note that this way may be genuinely cross-platform when tkinter is available.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020