Python Forum

Full Version: PyCharm SyntaxError: Invalid syntax
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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.
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)
    if not
filename.startswith('.'):
must be changed to:
    if not filename.startswith('.'):
Worked like a charm, thanks a lot Larz, you rock!