Roland's homepage

My random knot in the Web

Opening files from the command line

This Python script is a small helper to open files from the command line. It was inspired by a OS X utility of the same name.

A lot of my interaction with the files on my computers is done through a command-line shell, even though I use the X Window System. One of the things I like about the gvim editor is that it forks and detach from the shell it was started from. With other programs one usually has to explicitly add an & to the end of the command.

Then I read about the OS X open program, and I decided to write a simple program like it in Python.

The result is open.py. Note that it is pretty simple. This is by design. It has no options and it only opens files and directories. I have no intention of it becoming like OS X’s open or plan9’s plumb.

The open.py script can be downloaded from github. Currently, it requires the following programs;

The requirements can be easily adapted by editing the following dictionaries that are located near the top of the script;

filetypes = {'\.pdf$': ['mupdf'], '\.html$': ['firefox', '-new-tab'],
            '\.xcf$': ['gimp'], '\.[e]?ps$': ['gv'],
            '\.(jp[e]?g|png|gif|tif[f]?)$': ['gpicview'],
            '\.(pax|cpio|zip|jar|ar|xar|rpm|7z)$': ['tar', 'tf'],
            '\.(tar\.|t)(z|gz|bz[2]?|xz)$': ['tar', 'tf'],
            '\.(mp4|mkv|avi|flv|mpg|mov)$': ['mpv']}
othertypes = {'dir': ['rox'], 'txt': ['gvim', '--nofork']}

These dictionaries determine which utility is called based mostly on the extension (the last part of the filename).

In the filetypes dictionary, the keys (the strings before the colons) are python regular expressions that are matched to the filenames given on the command line. So for example, every while whose name ends in .pdf is opened with the mupdf program. The case of the letters is ignored when searching. The value for each item is a list of commands; the program to start and any extra arguments. The filename is appended to that list, and then the list is passed to subprocess.Popen. You can edit the list of commands to use your favorite applications.

The othertypes dictionary works slightly different. At this moment it has only two keys; dir for directories and txt for all files that are recognized as containing text by the file utility. You can substitute your favorite file manager and editor here.


For comments, please send me an e-mail.


Related articles


←  TeXcalc Managing configuration files with ‘deploy’  →