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

Avi Gross avigross at verizon.net
Tue Nov 6 22:20:52 EST 2018


I may be missing something but it looks like the embedded double quotes may be a problem in this:

cmd = "blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % ...

if you use single quotes as in:

cut -d"="

becomes 

cut -d'='

or escape the double quotes with \" and so on ...

The above seems to be seen as:

cmd=ALPHA=BETA % ...

where ALPHA="blkid -o export %s | grep 'TYPE' | cut -d"
and BETA=" -f3"

Other issues may also be there. 
-----Original Message-----
From: Tutor <tutor-bounces+avigross=verizon.net at python.org> On Behalf Of Alan Gauld via Tutor
Sent: Tuesday, November 6, 2018 7:37 PM
To: tutor at python.org
Cc: python-dev at python.org
Subject: Re: [Tutor] SyntaxError: can't assign to literal while using ""blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)" using subprocess module in Python

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


_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list