[Tutor] SyntaxError: can't assign to literal while using ""blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)" using subprocess module in Python
Steven D'Aprano
steve at pearwood.info
Wed Nov 7 18:21:14 EST 2018
On Wed, Nov 07, 2018 at 10:22:00AM +0100, srinivasan wrote:
> 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*?
Then get them working first. What happens when you execute that line
directly on the command line?
But I have to wonder, are you being paid by the hour? Because it seems
like a waste of time and effort to be using a pipeline of commands for
this. You seem to be deliberately turning a small, easy job into a huge,
difficult job.
The *easiest* way to do this is to do the string processing in Python,
not using the hard-to-use shell commands.
Python has its own grep module:
import grep
but since all you are doing is looking for a plain string with no
wildcards, you don't even need that. Collect the output of blkid into a
string, then do your processing in Python:
# Untested
for line in output.splitlines():
# each line looks something like this
# "/dev/md0: UUID="3cb91a07-14c9-4ae4-81e1-6eb662eabeee" TYPE="ext3"
p = line.find('TYPE="')
if p == -1:
continue # skip the line
result = line[p:]
if line.endswith('"'):
result = result[:-1]
print("Found file type", result)
Much easier to read and write and debug than a pipeline of Unix
command line tools, and likely to be faster too.
--
Steve
More information about the Tutor
mailing list