Open file with command-line program

I'm looking for a way to open a .py file with Python in a terminal every time a file is double-clicked in a GUI file manager, like in Windows. How would I go about doing this?

For Ubuntu 13.10

1

1 Answer

This works in Unity & Gnome, similar things are there for other desktop environments.

Right click on the file, and go to Properties, and permissions. Make sure this is ticked :

Execute - TICK Allow executing file as program
(You need this ticked anyway for the file to run in terminal, or be 'executed', it is the same as running chmod +x /PATH/TO/FILE)

And make sure this, under the Behaviour tab, in Nautilus file manager's preferences, is set to this:Executable Text Files - Ask Each Time

Now every time you click on it, you should get:

Do you want to run EXAMPLE.py, or show its contents - buttons

Run in Terminal, you can guess.
Display opens it in the default text editor defined in 'Open With...'. in the files preferences.
Run runs it in the background, with no terminal. This is also what happens if you select 'Run executable text files when they are opened' above.


Here is a fix to stop it closing instantly - go to Profile Preferences, and under Title and Command, set:

'When the command exits' to 'Hold the terminal open', Now when you run a script like this :

#!/usr/bin/python
import time
print "Hello"
time.sleep(5)
print "Goodbye"

(Remember the shebang #!/usr/bin/python line, otherwise it won't be interpreted correctly. You can also use #!/usr/bin/env python.)

It stay open a while, then you should get something like:enter image description here

5

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like