获取unicode输入,需要它是字符串吗

2024-05-14 14:49:32 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我今天终于抽出时间来写我的第一个程序了,除了一件事,一切都进展顺利。你知道吗

在下面的代码中,我让用户传入一个目录的路径。我本以为这是一个字符串,但在得到一个错误后,我设法找到了源代码。有什么问题吗?,我得到的是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\u input()获取输入。键入的目录如下所示:

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

现在我可能只是误解了错误的含义,因为这基本上是我第一次编程,但我发现了正确的错误。你知道吗

TypeError: 'builtin_function_or_method' object is not iterable

非常感谢您的帮助


Tags: thetopath目录youis错误files
1条回答
网友
1楼 · 发布于 2024-05-14 14:49:32

我不知道你为什么认为这和Unicode或字符串有关。错误信息非常清楚:您试图迭代实际的函数对象,而不是函数的结果。这是因为您实际上没有调用os.listdir:您只是将files设置为函数本身。要调用函数,请始终使用括号:

files = os.listdir()

今后,请也包括任何追溯你得到。这对调试至关重要。你知道吗

相关问题 更多 >

    热门问题