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 ...
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 ...
If you want to open whatever path the user chose in their preferred editor (or the system’s default for text files), avoid hardcoding gedit and use a cross-platform launcher. This works with any runtime path stored in a variable:
import os, sys, subprocess
def open_in_editor(path):
if not os.path.exists(path):
raise FileNotFoundError(path)
if sys.platform.startswith("win"):
os.startfile(path) # uses the associated editor
elif sys.platform == "darwin":
subprocess.Popen(["open", path]) # macOS
else:
editor = os.environ.get("EDITOR") or os.environ.get("VISUAL")
if editor:
subprocess.Popen([editor, path])
else:
subprocess.Popen(["xdg-open", path]) # desktop default on Linux Notes:
subprocess to avoid quoting issues and command injection. See the Python docs for subprocess and argument handling at docs.python.org/library/subprocess.html. os.startfile is documented at docs.python.org/library/os.html#os.startfile.xdg-open uses the desktop’s file associations and will launch gedit if it is the default; more at freedesktop.org/wiki/Software/xdg-utils.Gio.AppInfo API is documented at developer.gnome.org/gio/stable/GAppInfo.html.Jump to Post— jcao219 18I 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 doos.system("gedit file.txt"),
and you can also make …
Jump to Post— Gribouillis 1,391A good solution is
import webbrowser webbrowser.open("file.txt")On my system, it opens the file with gedit.
Jump to Post— Gribouillis 1,391It's very simple
myVariable = "file.txt" webbrowser.open(myVariable)
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) We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.