Bottom Page

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()
Quote
#2
Show us the whole traceback, please.
Quote
#3
You missed a close-paren on line 12.
Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more.

Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :)
Quote
#4
Missing parenthese in line 12.
Like this.
os.rename(file_name, filename.translate("0123456789"))
Quote
#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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Syntax Error: Invalid Syntax in a while loop sydney 1 74 Oct-19-2019, 01:40 AM
Last Post: jefsummers
  [split] Please help with SyntaxError: invalid syntax Mason 1 265 Apr-28-2019, 06:58 PM
Last Post: Yoriz
  SyntaxError: Invalid syntax in a while loop ludegrae 3 2,317 Dec-18-2018, 04:12 PM
Last Post: Larz60+
  SyntaxError: invalid syntax at run .py tuxo9999 10 1,625 Aug-23-2018, 03:58 PM
Last Post: Axel_Erfurt
  Homework: Invalid syntax using if statements chehortop 3 1,024 Mar-01-2018, 04:38 AM
Last Post: micseydel
  invalid syntax error penlemon 4 1,013 Jan-27-2018, 02:46 PM
Last Post: sparkz_alot
  Invalid syntax: string index out of range darkreaper1959 6 2,571 Jan-22-2017, 06:24 PM
Last Post: nilamo

Forum Jump:


Users browsing this thread: 1 Guest(s)