[Tutor] SyntaxError: can't assign to literal while using ""blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)" using subprocess module in Python

Alan Gauld alan.gauld at yahoo.co.uk
Tue Nov 6 19:37:25 EST 2018


On 06/11/2018 18:07, srinivasan wrote:

> bash command in python using subprocess module, I ma seeing the below
> *        cmd = "blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)*

In general you should try to do as little as
possible using bash and subprocess. Especially
try to avoid long pipelines since you are
starting a new OS process for every element
in the pipeline. That means, in your case,
you are running 4 processes to get your
result - Python, blkid, grep and cut

Python is designed to do much of what the shell
command can do almost as easily and much more
efficiently (no new processes being started).

In this case just execute the blkid bit in bash
because its too difficult to replicate simply
in Python. Then use Python to search for the
TYPE lines and slice them to size.

That will, in turn, simplify your command string
and remove the issue of multiple quotes.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list