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++

5
  • what do you mean by "launch the file"? in text editor? Commented Jun 22, 2018 at 6:33
  • yes, i am creating the file as *.txt want to open it with Notepad or Notepad++ Commented Jun 22, 2018 at 6:34
  • this doesn't answer my question, how your file should be launched? in what text editor? it is OS-dependent Commented Jun 22, 2018 at 6:36
  • i am using Windows platform, instead of default program i want to open it with another program. Commented Jun 22, 2018 at 6:40
  • Have a look at the subprocess module, especially subprocess.Popen. Commented Jun 22, 2018 at 7:58

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry to ask you, can you explain a bit please, i am using windows
@mortuzahasan Updated my answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.