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

Chris Angelico rosuav at gmail.com
Wed Nov 7 07:57:28 EST 2018


On Wed, Nov 7, 2018 at 11:42 PM srinivasan <srinivasan.rns at gmail.com> wrote:
>
> Some I managed to fix temporarily as below, might be useful for others. Also please correct me if anything wrong or for any improvements in the below
>
>         cmd = "blkid -o export %s" % partition_path
>         out = self._helper.execute_cmd_output_string(cmd)
>         var = out.split("TYPE=", 1)[1]
>         quoted = re.compile('(?<=^\")[^"]*')
>         for string in quoted.findall(var):
>             return string

Leaving aside the fact that MS Comic Sans is known to the State of
California to cause cancer, this code is probably okay if you don't
mind it being overengineered. Here's a much simpler version, albeit
untested:

out = subprocess.check_output(["blkid", "-o", "export", partition_path])
for line in out.split("\n"):
    item, value = line.split("=")
    if item == "TYPE": return value

No helper needed. Safe against command injection. Uses the known
format of the command's output; if you want other information as well
as the type, you could get that too.

ChrisA



More information about the Python-list mailing list