0

I am new to python. I'm getting an error while executing the code:** Given below:

 File "C:/Users/shashipau/PycharmProjects/FirstProject/rename.py", line 14, in <module>
    rename_files()
  File "C:/Users/shashipau/PycharmProjects/FirstProject/rename.py", line 10, in rename_files
    os.rename(re_file, re_file.translate(None, "0123456789"))
    TypeError: translate() takes exactly one argument (2 given)
   Process finished with exit code 1

My Code is :

import os

def rename_files():
    sxlist_file = os.listdir(r"D:\Python Te\PythomProgram\prank")
    os.chdir(r"D:\Python Te\PythomProgram\prank")
    save_path = os.getcwd()
    print(sxlist_file)
    for re_file in sxlist_file:
        os.rename(re_file, re_file.translate(None, "0123456789"))
        os.chdir(save_path)

rename_files()
Inconnu
  • 4,928
  • 2
  • 31
  • 41
  • 1
    What are you expecting that call to `translate` to do? – Daniel Roseman Nov 24 '16 at 17:35
  • 1
    if you're using Python 3, `str.translate()` takes only one argument https://docs.python.org/3/library/stdtypes.html#str.translate – ettanany Nov 24 '16 at 17:37
  • The error message is telling you exactly what's wrong... – Jordi Nebot Nov 24 '16 at 19:52
  • This code is from a course on Udacity. I am sure a lot of you know the code is to remove numerical values from files. In this case the instructors friends have played a prank on him. There is a directory with several pictures in it. Each picture is him holding a piece of paper with a letter on it. He can not unscramble the message unless they are alphabetized minus the numbers. I did have a problem at first. I was running the code under python 3 and it should be version 2 which lead me here. – J.Spivey May 01 '17 at 23:54

2 Answers2

0

As you can see in the method's documentation, translate() expects only one parameter. You are sending None and "0123456789" in re_file.translate(None, "0123456789").

Also, the method doesn't expect a string like you apparently think, as it's also stated in the docs.

The table must be an object that implements indexing via __getitem__(), typically a mapping or sequence.

To create this object, use the maketrans method.

lucasnadalutti
  • 5,380
  • 1
  • 22
  • 44
  • 1
    It would be helpful to mention that one would usually use [maketrans](https://docs.python.org/3/library/stdtypes.html#str.maketrans) to create the *table*, which then would be passed to *translate*. – idjaw Nov 24 '16 at 17:38
  • True, added that much. – lucasnadalutti Nov 24 '16 at 17:44
0

First of all, I feel the need to advise you on how bad of an idea it is to run this program. Even as a prank.

With that out of the way, this is a opportunity to learn about error messages. Let's take apart the error message to see what it is telling us.

 File "C:/Users/shashipau/PycharmProjects/FirstProject/rename.py", line 14, in <module>
    rename_files()

First we have the backtrace, this is not the error itself, but shows the calls that were made to get to the function which returned the error. The first line shows the file and line number of the function call. The second shows what the call was. From this we can tell that on line 14 of rename.py, rename_files() was called with no arguments.

 File "C:/Users/shashipau/PycharmProjects/FirstProject/rename.py", line 10, in rename_files
    os.rename(re_file, re_file.translate(None, "0123456789"))
    TypeError: translate() takes exactly one argument (2 given)

The next set of lines is where the error took place in this case. We can see the same information discussed in the previous section, plus the error message. The message is telling us that one extra argument was given to the function translate(). We can now look back at the line that the error returned on, and see that the function call to translate() has two arguments, None and "0123456789". Replacing these with a single argument with fix this error.

Once you have that error fixed, everything will still not work! If we take a look at the documentation, we can see the expected argument is a translation table which can be generated by calling str.maketrans(). So, your code could be changed to be the following:

import os

def rename_files():
    trans_table = str.maketrans("abcdefghij", "0123456789")
    sxlist_file = os.listdir(r"D:\Python Te\PythomProgram\prank")
    os.chdir(r"D:\Python Te\PythomProgram\prank")
    save_path = os.getcwd()
    print(sxlist_file)
    for re_file in sxlist_file:
        os.rename(re_file, re_file.translate(trans_table))
        os.chdir(save_path)

rename_files()

Please for the love of god, do not run this code though. Give it a single file to rename, do not scan for files. Especially when you are new to python, you might do something wrong and unintentionally rename a lot of files you did not intend to.