Python Forum
PyCharm SyntaxError: Invalid syntax - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: PyCharm SyntaxError: Invalid syntax (/thread-7108.html)



PyCharm SyntaxError: Invalid syntax - KasperMikkelsen - Dec-21-2017

Hi,

I'm going through the tutorial of the coding app "Mimo" on my iPhone. Part of the tutorial involves trying out the PyCharm Community Edition.

I am using a PC. I have written the following code:

____________________________________________________________________________________________________________________________________

import os
from datetime import datetime
folder = r"C:\Users\JakobS\Desktop\Photos for Python project"
location = input("Photo location:")
files = os.listdir(folder)
for filename in files:
if not
filename.startswith('.'):
file = os.path.join(folder,filename)
m_time = os.path.getmtime(file)
real_time = datetime.fromtimestamp(m_time)
f_time = datetime.strftime(real_time,"%Y%m%d_%H%M%S")
new_filename = f_time + "" + location + ".png"
new_file = os.path.join(folder,new_filename)
os.rename(file,new_file)

____________________________________________________________________________________________________________________________________

I get the following error, though:
____________________________________________________________________________________________________________________________________

C:\Users\JakobS\PycharmProjects\rename_files\venv\Scripts\python.exe C:/Users/JakobS/PycharmProjects/rename_files/renamer.py
File "C:/Users/JakobS/PycharmProjects/rename_files/renamer.py", line 7
if not
^
SyntaxError: invalid syntax

Process finished with exit code 1
____________________________________________________________________________________________________________________________________

Could anybody help me with this?

Thanks in advance

Kasper Mikkelsen


RE: PyCharm SyntaxError: Invalid syntax - Larz60+ - Dec-21-2017

First, please re-post code between code  tags, see BBCODE
Next, use shift-ctrl-v to paste code as it will preserve indentation (most important)

Your code cannot properly be tested until above is completed.


RE: PyCharm SyntaxError: Invalid syntax - KasperMikkelsen - Dec-21-2017

Sorry! Please find the code below, I hope this makes better sense.

import os
from datetime import datetime
folder = r"C:\Users\JakobS\Desktop\Photos for Python project"
location = input("Photo location:")
files = os.listdir(folder)
for filename in files:
    if not
filename.startswith('.'):
    file = os.path.join(folder,filename)
    m_time = os.path.getmtime(file)
    real_time = datetime.fromtimestamp(m_time)
    f_time = datetime.strftime(real_time,"%Y%m%d_%H%M%S")
    new_filename = f_time + "" + location + ".png"
    new_file = os.path.join(folder,new_filename)
    os.rename(file,new_file)



RE: PyCharm SyntaxError: Invalid syntax - Larz60+ - Dec-21-2017

    if not
filename.startswith('.'):
must be changed to:
    if not filename.startswith('.'):



RE: PyCharm SyntaxError: Invalid syntax - KasperMikkelsen - Dec-21-2017

Worked like a charm, thanks a lot Larz, you rock!