开发者

getting a list of files in a custom directory using glob()

Im trying to write a program that renames files when a use input their own custom file directory. I'm at a very early part of it. And this 开发者_运维问答is my first time using the OS and glob commands. My code is below. However when I tried running that, the result was an empty list. I tried typing a file root directory into the glob command directly, it somehow works, but the result isn't what I wanted.

Hope you guys can help me. Thanks.

import os, glob
def fileDirectory():
    #Asks the user for a file root directory
    fileroot = raw_input("Please input the file root directory \n\n")

#Returns a list with all the files inside the file root directory filelist = glob.glob(fileroot) print filelist

fileDirectory()


Python is white-space sensitive, so you need to make sure that everything you want inside the function is indented.

Stackoverflow has its own indentation requirements for code, which makes it hard to be sure what indentation your code originally had.

import os, glob
def fileDirectory():
    #Asks the user for a file root directory
    fileroot = raw_input("Please input the file root directory \n\n")

    #Returns a list with all the files inside the file root directory
    filelist = glob.glob(fileroot)
    print filelist
fileDirectory()

The next thing is that glob returns a the results of a glob - it doesn't list a directory, which appears to be what you're trying to do.

Either you want os.listdir, or os.walk or you actually should ask for a glob expression rather than a directory.

Finally raw_input might give you some extra whitespace that you'll have to strip off. Check what fileroot is.

You might want to split up your program, so that you can investigate each function separately.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜