Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python cross platform os.system

Tags:

python

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?

like image 467
user1675111 Avatar asked Sep 16 '25 22:09

user1675111


2 Answers

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&')
like image 69
Tadgh Avatar answered Sep 19 '25 15:09

Tadgh


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)
like image 35
Abgan Avatar answered Sep 19 '25 14:09

Abgan