Syntax Error Near Unexpected Token '(' Python Script

bash: syntax error near unexpected token `(' - Python

Are you typing this at the unix command prompt? You should be doing this inside the python environment, ie. type python at the prompt and work from there.

Also, no ; needed at the end of the line in Python

Running a Python program returns: bash: syntax error near unexpected token '('

From the imgur image, the problem is:
Sample Image

Instead of:

python3 Camera Controller for Raspberry Pi (Part 4) .py

use:

python3 'Camera Controller for Raspberry Pi (Part 4) .py'

Without the quotes, the shell considers Camera, Controller, for, Raspberry, Pi, (, Part, 4, ), and .py all to be separate. Because ( is shell metacharacter in an illegal position, the shell cannot parse this command line.

With quotes, the whole of the file name is treated as one argument and is passed to python unharmed.

After making this change, there may very well be additional issues with the python code. As kdheepak noted, there may be problems with an import statement. For instance, the code imports from a module sh but my python installation does not include any module by that name.

syntax error near unexpected token `(' python script

The Python file needs to be executed by the Python interpreter.

You can do e.g.:

python script.py

where script.py is the name of your file.

What you are doing instead is running your Python script through Bash (in fact, what you are getting is a typical Bash error). Probably this is happening because you are using ./script.py, but your script is missing the correct shebang line:

#!/usr/bin/env python

Indeed, if yours is a Python 3 script, you should use python3 instead of python.

getting syntax error near unexpected token `;' in python

The problem is caused by this line:

    cmd = 'ls -l ' + line

it should be modified to:

    cmd = 'ls -l ' + line.strip() 

When you read the line from your text file, you also read the trailing \n. You need to strip this so that it works. The getstatusoutput() doesn't like the trailing newline. See this interactive test (which is how I verified it):

In [7]: s, o = commands.getstatusoutput('ls -l dummyFile')

In [8]: s, o = commands.getstatusoutput('ls -l dummyFile\n')
sh: Syntax error: ";" unexpected

Python - syntax error near unexpected token `|'

This is really a problem with shell not python. The syntax error is coming from sh:

sh: -c: line 1: syntax error

As you observe, removing the whitespace fixes your problem.

What is happening is that the output of the first os.popen() is assigned to file as a Python string, and that string includes a terminating newline.
That's because the output of the command includes a terminating newline.

So file will be the string "foo-access.0\n" or something similar.

That means that the command sent to the second os.popen() is

sudo cat foo-access.0
| wc -l

which is not valid shell syntax (generally, pipe, |, cannot begin a line).

Removing the whitespace removes the trailing newline, so the command sent to the second os.popen() is

sudo cat foo-access.0 | wc -l

which is all good.

As others have noted, this script is poor in other ways too.

Syntax error near unexpected token ` Hello World! ' on Ubuntu

This error comes from bash, not Python. As if you were to type print("Hello World") in your terminal. Try running the python file as python3 file3.py.



Related Topics



Leave a reply



Submit