[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
Wed Nov 7 21:34:43 EST 2018


It is easier to understand code when it is shown unaltered. The character
"*" (as in asterisk) has plenty of meaning in many languages and especially
in python. 

WHY is it being shown in your snippet at beginning and end?

*"cmd = "blkid -o export %s | grep \'TYPE\' | cut -d\"=\" -f3" % fs"*,

Is it there as a form of emphasis or markup? It dos NOT belong when showing
others and asking for help when every symbol (not to mention indentation)
can have other meanings or make it nonsensical.

The simplest way for you to debug is to do a step at a time and check.
Before asking for help, it can be a goof learning experience to try each
step out using print statements or other techniques.

In your case, have you tried the overall command directly from the shell?

I won't do much more but start what you could have done.

Removing the two asterisks and the strange added comma I see:

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

But why is it all in quotes?

Removing the quotes:

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

OK, to test this I need a value for fs to be interpolated into the string.

Here is my output:

>>> fs = "TESTING"
>>> cmd = "blkid -o export %s | grep \'TYPE\' | cut -d\"=\" -f3" % fs
>>> print(cmd)
blkid -o export TESTING | grep 'TYPE' | cut -d"=" -f3

Is that the command line you wanted? If so, you can continue by yourself. If
not, you need to fix it before continuing.

But, as others have noted, there really is not much reason to do this using
a shell script with pipes if you know enough Python including some added
modules.

You remind me of a supervisor years ago who did not have a computer science
background but decided he wanted to run some scripts that executed remote
commands to gather billing files and statistics from many machines including
(would you believe it) some in places like Japan over 300 baud connections.
He wrote some humungous scripts for ksh (Korn Shell) that did something like
what you are doing and worse. Lots of loops that iterated over each machine
then each billing file with pipelines as long as 10 pipe symbols in a
command line. Lots of use of grep to match patterns like the first character
of a line, then programs like cut to grab a subset of columns (separated by
pipe symbols, of course) and so on. 

The program ran for hours in middle of the night for the US but made me get
calls from Japan and Israel complaining about how sluggish their machines
were doing their busy hour. So, I rewrote the entire script he used as
something that gathered the raw files to a US machine and called a single
AWK script that sliced and diced very efficiently.

Python is even more powerful and what you want, perhaps not trivial, is
straightforward.

I can't say it has built-in methods to display available information about
block devices. But you can spawn a simpler command to run just blkid with
output redirected to a pipe in your process. (Alternately you can direct it
to a file then open that.)

Not sure what you want but is your output a bit like this?

ID_FS_UUID=7fa9c421-0054-4555-b0ca-b470a97a3d84
ID_FS_UUID_ENC=7fa9c421-0054-4555-b0ca-b470a97a3d84
ID_FS_VERSION=1.0
6-
ID_FS_USAGE=filesystem

You can then have a loop iterating over the lines with variable "line" each
time being the holder of one line worth of info.

You want only lines that contain "TYPE" and for a fixed string like that you
don't need a grep variant. 
You want to segment it based on "=" symbols? Try:

line.find("TYPE")

If that returns a find, continue. (-1 if not found.)

You want to now split that line into multiple parts based on the "=" symbol.

pieces = line.split("=") 

and the results now are a list of strings. But recall the first piece in
Python has a 0 index. So your -f3 would be:

pieces[2]

I won't continue with the rest of your code but if the line you really want
is like the one above:

ID_FS_TYPE=ext4

You can pretty much do all the above very compactly using a regular
expression that (in English) matches a line containing

Anything followed by exactly "TYPE=" followed by open paren, anything to end
of line, and close paren. The result of any match will be available to you
as the first and only part of the pattern you asked for. You can work out
details. Probably less efficient but not very much code. 

And the string method can be compacted in many ways. I mean code like
line.split[2] or in your case if there is a match for line.find() for
"TYPE=" then you can index the return value by adding the 5 characters that
matched:

>>> line = "ID_FS_TYPE=ext4"
>>> line.find("TYPE=")
6
>>> line[6+5:]
'ext4'













-----Original Message-----
From: Tutor <tutor-bounces+avigross=verizon.net at python.org> On Behalf Of
srinivasan
Sent: Wednesday, November 7, 2018 4:22 AM
To: tutor at python.org; python-list 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

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