Error: TypeError: translate() takes one argument (2 given)

耗尽温柔 提交于 2019-12-13 07:20:18

问题


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()

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/40792038/error-typeerror-translate-takes-one-argument-2-given

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!