Reputation: 9171
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
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
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
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