0

所以我今天终于开始写我的第一个程序,除了一件事,一切都很顺利。

在下面的代码中,我让用户传入目录的路径。我原以为这是一个字符串,但在收到错误后我设法找到了源。问题?,我得到了 unicode,当它用于获取目录中的文件列表时,这似乎会导致错误。

print "Hello Welcome to my little Porgram"
print "I need a little information to rename the files"
usd=getuserin("What is the file path to the files that you wish to rename?")
print "Thank you for chosing a directory path"
print "The directory path you chose was:" + " " + usd
mainname=getuserin("What is the name of the TVshow/Anime/Other thing? ")
print "Okay so its called" + " " + mainname
print "Okay I'll start renaming right away"
renamefiles(usd, mainname)

第三行是返回 Unicode 的那一行,基本上它所做的就是通过 raw_input() 获取输入。输入的目录如下:

def renamefiles(directory, Mainname) :
    os.chdir(directory)
    files=os.listdir
    for elem in files:

现在我可能只是误解了错误的含义,因为这基本上是我第一次编程任何东西,但我发现了正确的错误。

TypeError: 'builtin_function_or_method' object is not iterable

非常感谢任何帮助

4

1 回答 1

3

I've no idea why you think this has anything to do with Unicode or strings. The error message is quite clear: you're trying to iterate through an actual function object, rather than the result of a function. This is because you haven't actually called os.listdir: you've just set files to the function itself. To call a function, always use parentheses:

files = os.listdir()

In future, please also include any traceback you get. That is vital for debugging.

于 2012-12-08T17:45:22.403 回答