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

srinivasan srinivasan.rns at gmail.com
Wed Nov 7 04:22:00 EST 2018


After changing the line to *"cmd = "blkid -o export %s | grep \'TYPE\' |
cut -d\"=\" -f3" % fs"*, Now I dont see the error "SyntaxError: can't
assign to literal"
This is not returning exactly "*vfat*" instead of this, it is returning as "*
/dev/mmcblk1p1: LABEL="efi" UUID="1084-AA42" TYPE="vfat"* "

Could you please help me  as it seems to be like grep and cut commands are
not working in the above line ie., on *cmd = "blkid -o export %s | grep
\'TYPE\' | cut -d\"=\" -f3" % fs*?

On Wed, Nov 7, 2018 at 9:41 AM Avi Gross <avigross at verizon.net> wrote:

> 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
>
> _______________________________________________
> 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