Python Forum
Invalid Syntax problem (?!?)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Invalid Syntax problem (?!?)
#1
Hello, I am just starting to learn Python with the Python 3.5.2 version. I am supposed to start with a really easy task, renaming all the files in a folder. (Taking out the numbers out of the filenames.)

But I always get a Syntax-Error message (Invalid Syntax). Can someone please help me? I don't know what is wrong!

line 13: os apparently is a syntax error
import os

def rename_files():
    #(1) get file names form a folder
    file_list = os.listdir(r"C:\Users\harley\Documents\Python\Introduction\rename")
    #print(file_list)
    saved_path = os.getcwd()
    print("Current Wokring Directory is", saved_path)
    os.chdir(r"C:\Users\harley\Documents\Python\Introduction\rename")
    #(2) for each file, rename filename
    for file_name in file_list:
        os.rename(file_name, filename.translate("0123456789")
    os.chdir(saved_path)

rename_files()
Reply
#2
Show us the whole traceback, please.
Reply
#3
You missed a close-paren on line 12.
Reply
#4
Missing parenthese in line 12.
Like this.
os.rename(file_name, filename.translate("0123456789"))
Reply
#5
Hello!
In Python 3 str.translate() works a bit different.
Python 3.5.2 (default, Sep 10 2016, 08:21:44) 
Type "copyright", "credits" or "license" for more information.

IPython 5.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: file_name = '0012ow32jioo123.hh1'

In [2]: table = {}

In [3]: for key in range(10):
   ...:     table[ord(str(key))] = None
   ...:     

In [4]: table
Out[4]: 
{48: None,
 49: None,
 50: None,
 51: None,
 52: None,
 53: None,
 54: None,
 55: None,
 56: None,
 57: None}

In [5]: new_file = file_name.translate(table)

In [6]: new_file
Out[6]: 'owjioo.hh'
I suppose you want to do this on the base name of the file not to an extension too. As you see is 'hh' now. This will ruin your mp3 collection  Smile
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Invalid syntax Slome 2 1,910 May-13-2022, 08:31 PM
Last Post: Slome
  print(f"{person}:") SyntaxError: invalid syntax when running it AryaIC 11 16,091 Nov-07-2020, 10:17 AM
Last Post: snippsat
  Invalid syntax error, where? tucktuck9 2 3,378 May-03-2020, 09:40 AM
Last Post: pyzyx3qwerty
  Invalid syntax defining a dictionary? ep595 6 5,020 Nov-19-2019, 08:06 PM
Last Post: ThomasL
  Syntax Error: Invalid Syntax in a while loop sydney 1 4,027 Oct-19-2019, 01:40 AM
Last Post: jefsummers
  [split] Please help with SyntaxError: invalid syntax Mason 1 2,144 Apr-28-2019, 06:58 PM
Last Post: Yoriz
  SyntaxError: Invalid syntax in a while loop ludegrae 3 14,609 Dec-18-2018, 04:12 PM
Last Post: Larz60+
  SyntaxError: invalid syntax at run .py tuxo9999 10 7,119 Aug-23-2018, 03:58 PM
Last Post: Axel_Erfurt
  Homework: Invalid syntax using if statements chehortop 3 3,594 Mar-01-2018, 04:38 AM
Last Post: micseydel
  invalid syntax error penlemon 4 3,776 Jan-27-2018, 02:46 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020