4

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?

1
  • 1
    if sys.platform == 'win32': os.system('START notepad blabla'), elif platform == 'linux2'... Commented Sep 25, 2012 at 14:07

3 Answers 3

4

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&')
Sign up to request clarification or add additional context in comments.

4 Comments

do I use if os.name =='nt' or 'win32'?
test it out on your system. I just ran it on both my windows and my ubuntu box. os.name came up as 'nt' and 'posix' respectively :)
okay. and just to clarify, for the windows one is it os.system('notepad ecryptedText.txt&') OR os.system('START notepad blabla')
notepad is automatically in the path for windows installations. You do not need to do START notepad unless for some crazy reason you lost a crucial bit of path.
4

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)

Comments

0

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.

1 Comment

To nitpick, gedit may very well work on Windows if it only depends on GTK+ or something. Of course, that doesn't mean anyone actually uses it there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.