简体   繁体   中英

Getting a unicode input, need it to be a string

So I finally got around to writing my first program today and everything is working out smoothly except for one thing.

In the code below, I have the user pass in a path to a directory. I was expecting this to be a string but after getting an error I managed to find the source. The problem?, I'm getting unicode which seems to be causing an error down the line when its used to get a list of the files in the directory.

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)

The 3rd line is the one that is returning the Unicode, basically all it does is get the input through raw_input() . The directory that was typed in goes to this below:

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

Now I could just be misinterpreting what the error means as this is basically my first time programming anything but I that I have found the right error.

TypeError: 'builtin_function_or_method' object is not iterable

Any help is very very appreciated

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM