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

Dear Python Experts Team, As am newbie to python development, I am trying to use the below function to get verify the filesystem type of the SD card parition using bash command in python using subprocess module, I ma seeing the below Error "SyntaxError: can't assign to literal" *CODE:* *====* import helper from os import path import subprocess import os import otg_ni class emmc(object): """ emmc getters and setters info: https://www.kernel.org/doc/Documentation/cpu-freq/user-guide.txt """ def __init__(self): self._helper = helper.helper() self._otg_ni = otg_ni.otg_ni() *def get_fstype_of_mounted_partition(self, fs):* """ Get the filesystem type of the mounted partition. :partition_name : Partition path as string (e.g. /dev/mmcblk0p1) :return: filesystem type as string or None if not found """ * cmd = "blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)* *return self._helper.execute_cmd_output_string(cmd)* *def execute_cmd_output_string(self, cmd, enable_shell=False):* """ Execute a command and return its output as a string. :param cmd: abs path of the command with arguments :param enable_shell : force the cmd to be run as shell script :return: a string. """ try: result = subprocess.check_output(split(cmd), stderr=subprocess.STDOUT, shell=enable_shell) except subprocess.CalledProcessError as e: s = """While executing '{}' something went wrong. Return code == '{}' Return output:\n'{}' """.format(cmd, e.returncode, e.output, shell=enable_shell) raise AssertionError(s) return result.strip().decode("utf-8") *if __name__ == "__main__":* m = emmc() * m.get_fstype_of_mounted_partition("/dev/mmcblk0p1")* *Error:* *======* root:~/qa/test_library# python3 sd.py File "sd.py", line 99 * cmd = "blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)* * ^* *SyntaxError: can't assign to literal* root:~/qa/test_library# Kindly do the needful as early as possible, as am stuck with this issue from past 2 days no clues yet, please redirect me to the correct forum if this is not the right place for pasting python related queries Many Thanks in advance, Srini

On Wed, Nov 7, 2018 at 5:11 AM srinivasan <srinivasan.rns@gmail.com> wrote:
As am newbie to python development, I am trying to use the below function to get verify the filesystem type of the SD card parition using bash command in python using subprocess module, I ma seeing the below Error "SyntaxError: can't assign to literal"
This is more appropriate for python-list than for python-dev, which is about the development OF Python. But your problem here is quite simple:
cmd = "blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)
You're trying to use the same quotation mark inside the string and outside it. That's not going to work. Try removing your inner quotes. You may also want to consider using Python to do your searching and cutting, rather than a shell pipeline. For more information on that, ask on python-list; people will be very happy to help out. ChrisA

*def get_fstype_of_mounted_partition(self, fs):* """ Get the filesystem type of the mounted partition.
:partition_name : Partition path as string (e.g. /dev/mmcblk0p1) :return: filesystem type as string or None if not found """
* cmd = "blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)* *return self._helper.execute_cmd_output_string(cmd)*
root:~/qa/test_library# python3 sd.py File "sd.py", line 99 * cmd = "blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)* * ^* *SyntaxError: can't assign to literal* root:~/qa/test_library#
looking at cmd = "blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)* It’s probably because you have “ characters that are inside “ characters and it can’t tell where the string ends. It looks like you are trying to do cmd = "blkid -o export %s | grep 'TYPE' | cut -d” = " -f3" % (fs)* which doesn’t make sense. Try using triple quotes instead so it’s clear what string you are trying to use. cmd = “""blkid -o export %s | grep 'TYPE' | cut -d"=" -f3”"" % (fs) — David Rock david@graniteweb.com

This list is for the development *of* the Python interpreter, not support for *using* Python. If you signed up to this mailing list via the web, it clearly says: Do not post general Python questions to this list. highlighted in a great big box in red. Was that not clear enough? What can we do to make that more clear? You can try many other places for support, such as Stackoverflow, Reddit's /r/LearnPython, or the Python tutor mailing list: https://mail.python.org/mailman/listinfo/tutor but I expect most of them will tell you the same thing. You should try to give a *minimum* example of your problem, not your entire script. Start by reading here: http://sscce.org/ As for your SyntaxError, the problem is line 99:
File "sd.py", line 99 * cmd = "blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)* * ^* *SyntaxError: can't assign to literal*
The arrow ^ is pointing to the wrong equals sign, but simplifying the code and adding spaces makes it clear: cmd = "spam" = "eggs" gives the same error. Perhaps you mean == (equals) or perhaps the second assignment shouldn't be there at all? -- Steve
participants (4)
-
Chris Angelico
-
David Rock
-
srinivasan
-
Steven D'Aprano