Hello friends,
I am writing an Application in pygtk ,there i got stuck up with this problem
I want to open a text file using any editor using python

Can any give some solution to this please ...

Thanks in Advance ...

Recommended Answers

All 10 Replies

I want to open the file in an editor ..
jst like whn u give $ gedit file.txt in terminal .. i want the same to be done using python..

is it possible ???

I want to open the file in an editor ..
jst like whn u give $ gedit file.txt in terminal .. i want the same to be done using python..

is it possible ???

well, there is os.system,
so you can do os.system("gedit file.txt") ,
and you can also make it detect windows, and so it will do os.system("notepad file.txt") , but another way is to make a GUI text editor in pygtk and open that, but that takes more work.

A good solution is

import webbrowser
webbrowser.open("file.txt")

On my system, it opens the file with gedit.

Thanks for the solution ..
actually ... the output of my application is stored in a file which is given by the user during runtime
so the path of the file is stored in a variable.
now i want to open the file using the variable which stores the complete path of the file ..

According to the above mentioned solutions .. that is possible if the file name is known to us before ...

So,how can i do it now ?

It's very simple

myVariable = "file.txt"
webbrowser.open(myVariable)

yes ... that is coming in webbrowser .... thanks for that :)

but i want to open it in an editor like gedit ..

In windows that webbrowser command openned file I put in argument in my Notepad2, configured text editor.

Thanks, I was suffering for same missing as I wanted to have program to generate answers in fast speed and then open them in text editor. That is better solution than letting the results to scroll too fast to read.

Of course it is easy to open simple tk window with ScrolledText widget, but anyway..

Thanks, Griboulis!

yes ... that is coming in webbrowser .... thanks for that :)

but i want to open it in an editor like gedit ..

It may depend on your version of python. For python 2.6 on my gnome desktop, the webbrowser module opens text files with gedit. Otherwise, you can use

import subprocess as sp
myVariable = "file.txt"
sp.Popen(["gedit", myVariable])

Or perhaps you could do this:

import os
var = 'test.txt'
os.system('gedit '+var)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.