Reputation: 31
I am using Ubuntu 12.04 on a 64bit laptop. I am trying to open an application using python code.
import os
os.system("open /home/utsav/ab.txt")
It gives the following error:
"Couldn't get a file descriptor referring to the console 256"
What command do I want to use?
Upvotes: 3
Views: 2193
Reputation: 11
I tried this one. it works for me.
import os
os.system("command of your preferred app")
the command for app can be found from Desktop Entry tab of Properties of the preferred app.
Upvotes: 1
Reputation: 1059
For what it's worth, the Python 2.7.3 documentation says that the os
module is deprecated and the subprocess
module should be used instead. To execute a command this way you can use subprocess.call(args, ...)
("Using the subprocess module").
Based on the earlier answers you can use open
for Mac OS X and gnome-open
for a Linux distro running the Gnome desktop environment. (I checked gnome-open
and xdg-open
and both work on Fedora 16.) Windows is a bit tricky.
For Windows you need to use start
, but if there are spaces in the path to the file or the filename, then it doesn't work right. Quoting the filename doesn't quite fix it, either, since start
expects an unmarked argument with quotes around it to be a title (e.g. for a new cmd
interface window). This is a problem since neither the title nor the file to open is a marked argument, so to get the call to work right you have to do something like start "DummyTitle" "Filename with spaces.ext"
.
So what we've got is:
Mac OS X: subprocess.call(['open','/path/to/file'])
Linux running Gnome DE: subprocess.call(['gnome-open','/path/to/file'])
Windows: subprocess.call(['start','"DummyTitle"','"C:\\Path\\to\\File.ext"'])
Or something like that.
For versions of Python before 2.7.3, you could us the os
module with these args as strings as you suggest in your question (just altering the command portion of the system call). Note as well that there's os.startfile('/path/to/file')
for Windows which will open the file with whatever the associated default is for that filetype, if you are going the deprecated os
route.
Note that we still don't have a confirmed way of opening files on OSes using other desktop environments. Please suggest improvements to this answer!
Upvotes: 0
Reputation: 106430
The command you're using - open
is actually another command referred to in the man pages as openvt
, which opens a virtual terminal.
I don't think that's what you want to do, so you would want to use another command (such as gnome-open, xdg-open, geany, gedit, vim, or nano).
Upvotes: 1
Reputation: 309929
open
works on OS-X, under ubuntu I end up using gnome-open
(I don't know what the corresponding command is if you're using the k-desktop).
EDIT
based on the comment by Niklas B., you could probably also try xdg-open
.
Upvotes: 0