user1675111
user1675111

Reputation: 9171

Python cross platform os.system

currently my Python program opens a text file like this:

os.system('gedit decryptedText.txt&')

Now, I presume this will not work on Windows, since gedit is a Linux application? How can I make this run on both Windows and Linux. Or will it work on both?

Upvotes: 4

Views: 1940

Answers (3)

Abgan
Abgan

Reputation: 3716

On MS Windows you could use os.startfile(filename) for file types that have associated editors.

Hence your full solution would be something like:

def start_file(filename):
    if os.name == 'nt':
        os.startfile(filename)
    else:
        os.system('gedit %s&' % filename)

Upvotes: 4

Tudor Vintilescu
Tudor Vintilescu

Reputation: 1460

It will work on both, obviously, since gedit is the standard editor for the universe.

Kidding. It will not work, since you are essentially launching a specific application, only available on certain platforms (Linux). You could configure your default editor start command in a configuration file and use it to compose your command string.

Upvotes: 0

Tadgh
Tadgh

Reputation: 2049

Check for OS first, and assign depending on result?

if os.name == 'nt':
    os.system('notepad ecryptedText.txt&')
elif os.name == 'posix':
    os.system('gedit decryptedText.txt&')

Upvotes: 4

Related Questions