Invalid syntax message without any apparent error

Asked

Viewed 819 times

0

I am trying to rename a folder that has a set of files, I want to remove all digits of the names of the files. But when I try to run the code the error message appears: "invalid syntax", pointing specifically to the module os. Can anyone tell me the reason for the mistake and how to fix it? Follow the code:

import os 
def rename_files():
    #(1) pegue os nomes dos arquivos da pasta 
    file_list = os.listdir(r"C:\Projetos\arquivos") 
    #print(file_list)
    saved_path = os.getcwd()
    print("Curret Working Directory is "+saved_path)
    os.chdir(r"C:\Projetos\arquivos")
    #(2) para cada arquivo, renomeie o nome do arquivo
    for file_name in file_list:
        print("Old Name - "+file_name)
        print("New Name - "+file_name.translate(None, "0123456789")
        os.rename(file_name, file_name.traslate(None, "0123456789"))
    os.chdir(saved_path)
rename_files()
  • Can you put the full error message in the question? And how is this code running?

  • This is the complete error message. I run the program and open a window with the phrase "invalid syntax" I use idle to write, I don’t know if it influences how messages are shown.

  • @Andersoncarloswoss , I used another online editor and wrote the message: "Traceback (recent call last): File "python", line 13 os.Rename(file_name, file_name.traslate(None, "0123456789") Syntaxerror: invalid syntax"

1 answer

1


Error is on line 12, you did not close parentheses of print function:

The right thing would be:

import os 
def rename_files():
    #(1) pegue os nomes dos arquivos da pasta 
    file_list = os.listdir(r"C:\Projetos\arquivos") 
    #print(file_list)
    saved_path = os.getcwd()
    print("Curret Working Directory is "+saved_path)
    os.chdir(r"C:\Projetos\arquivos")
    #(2) para cada arquivo, renomeie o nome do arquivo
    for file_name in file_list:
        print("Old Name - "+file_name)
        print("New Name - "+file_name.translate(None, "0123456789"))//O erro estava aqui
        os.rename(file_name, file_name.traslate(None, "0123456789"))
    os.chdir(saved_path)
rename_files()
  • 1

    I closed now, but other errors appeared. Obg by indicating.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.