1

I did the search but i couldn't find any help, apologies if i my question is duplicate.

i am writing the code with python 3.6 and in windows environment.in my code, i opened a text file, write the data and close the file.

self.fileName = 'file path'
self.log_file = open(self.fileName, 'w')
self.log_file.write('Write results')
self.lof_file.close()

Instead of the user goes to file path and click to open it, i want to launch the file automatically after python save it.

how do i do that? please help

EDIT:

os.startfile(filepath=self.fileName)

command is working fine, but its opening with default program which is Notepad, how to open the file with specific program, for example, Notepad++

1 Answers1

1

If you know the command line way of doing it, you can use the os module as follows:

import os
self.file = 'file path'
self.log_file = open(self.fileName, 'w')
self.log_file.write('Write results')
self.lof_file.close()
os.system('gedit <file_path>')    # for ubuntu, gedit is generally present

For Windows, you can use:

import os
os.startfile('C:\\Users\\RandomUser\\Documents\\test.txt') 

Check this answer for more details: https://stackoverflow.com/a/15055133/9332801

dodobhoot
  • 493
  • 6
  • 15