简体   繁体   中英

Python syntax error near unexpected token

I checked several duplicate questions, but they are not related to python.

This is my script

#/bin/python3
import time

# Get the today date and use it as folder name
print(time.strftime("%Y-%m-%d"))

as soon as I try to use it form the terminal (bash shell), I get the syntax error.

./test.py: line 5: syntax error near unexpected token `time.strftime'
./test.py: line 5: `print(time.strftime("%Y-%m-%d"))'

Looking at the code nothing seems to be wrong, and I'm not getting which one is the problem and if it's related to Python or to Linux (as in other posts seems it can depends from linux too).

You're missing the ! in the first line, so it's just a comment and the whole thing is being interpreted as a sh script. It should look like this:

#!/bin/python3

Change first string to something like #!/usr/bin/python3

You should use #! instead of # .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM