Python module for shell commands - python

I've got a bash script that's starting to cross that grey area where it should probably be rewritten in a scripting language. Since I'm constantly tweaking the script bash is perfect because of how concise things are. Mostly I'm manipulating output from one program to put into another program (i.e. using cat, grep, sed, tail, head). I'm familiar with Python so I'm looking for a module that can essentially perform these commands. I've found some stuff that uses subprocess to call bash commands but I don't want a wrapper for bash commands. I also realize I could simply take the time to write most of these commands in python and even chain them fairly easily if I encapsulated them all into one class. It seems like such an obvious thing though I find it hard to believe these utilities don't exist in a module that already exists.

You can use the os module in python to have a command executed exactly as it would be in a command shell.
For example, if you have a file named "junk.txt" and want to use the "head" command on it, you would write head junk.txt in your shell.
In Python, it's
import os
os.system("head junk.txt")

Related

How to execute Scitools Understand commands from python

I want to execute Scitools Understand commands from python. It has its own shell which can be run using $ ./und and switches to und>. I want to run its commands from python. Any idea how can I do that?
First you should consider using the Python API for Ada Understand. This will be more comfortable than talking to the Understand console.
But using the console application is also possible. Ada Understand uses the standard input and output. Assuming you use Linux I have just a small demonstration for this.
create a file und.input with the contents
help
exit
And then exeucute this command
cat und.input | /opt/scitools/bin/linux64/und > und.output
This will create you the output into the file und.output.
You will not do it through files. With these files I just tried to give you evidence and a feeling that really the standard input and standard output is used. Knowing this, you need a way in Python to start a process and communicate to it over stdin and stdout. The subprocess module is a way to implement this. But there may be alternatives.
Although my demonstration above is on Linux, the Python implementation with subprocess will be the same for Windows.

Calling to python interactive script from another script

I am using Python 2.7
I have interactive Python script, named A, written using Cmd module.
However, sometimes there is a need to activate script A from other Python scripts.
It also could be nice if the interactive script A wouldn't exit in between commands given to them.
I tried to search for solutions and I found couple here
One is to pass a file from which the interactive script will read commands. It works well for me but it is not much liked.
Another is to use echo which doesn't work for me (still trying).
I wonder whether there are other solutions.....

Running Fiji script in python

I am trying to get a home made Fiji script to sun inside Python by calling Fiji, but there's little documentation on how to do it.
What I need is something like this:
def myfijiscript:
[CODE]
and then in Python:
fiji(myfijiscript)
is there a way to do this?
Python (or, to be precise, Jython) scripts within Fiji are executed using the org.python.util.PythonInterpreter class (see source code).
It doesn't make much sense to run a Jython script within a Java instance that is started from with Python, but have a look at those two questions concerning how to run external commands in python. You can save your script in a file myscript.py and then do:
call(["./ImageJ-linux64", "myscript.py"])
using the ImageJ launcher from the command line.
The other way is to use ImageJ as a library and just import the classes you need for your script, as others have suggested:
from ij import IJ

Automating Subversion Commands using Python

I am pretty new to Subversion, and not that experienced in Python, but am doing some work with large volume of media-files that need moving around within the directory. Using the Visions GUI, some of the file transfers are taking a very long time, so I'd like to automate these tasks to run over night by storing the actions within a text file and then having a python script act on these overnight?
For example the text file might contain a command such as:
svn mv current desired
How can I send this string to Terminal to execute the command?
You could do os.system call or try using PySVN, which may give you more control in Python over SVN repository you're working with.
The subprocess module is the best way to execute commands. As #Abgan points out, the better way might be to use a subversion library instead.
If you're on Windows, it'd be better to use an SVN library. On Linux/Mac/Unix you could go either way realistically, because these can run a subprocess well - windows doesn't do terribly well at this.
subprocess is indeed preferred over os.system today.
The nice thing about using subprocess.Popen instead of an SVN library (module), is that you don't have to learn two ways of accessing SVN. Your command line SVN knowledge translates directly into your code.

How to execute a series of Python scripts in batch?

I frequently find that I need to run a series of python scripts sequentially. The order and number of these scripts may vary, but instead of executing each script individually and sequentially, is there a preferred way of batch automating a series of Python scripts? My programs require multiple and complex file path and other argument combinations.
Refactor them into modules, then make a new script that imports the relevant functions from each module and runs those.
The easiest way to do this is to write a main function per module and run all the main function serially.
I think larsmans' option is the best by far. But if there is a reasons you cannot or should not turn them into easily imported modules, it is quite easy to just make a .bat file in Windows (or .ps1 if you have powershell) which calls each of them in turn. In Linux, the same thing can be done with bash or csh as long as you remember to make the resulting batch executable.
If you want another python script that calls them as though they were being called from a command line (in order to pass in the argument combinations without refactoring them into modules) you can readily use os.system or the more powerful (but sligtly more complicated) subprocess module.

Categories

Resources