python读取目录下的内容,包含子目录和文件夹
#!/usr/bin/env python # # [代码名字: List Directories Content] # [代码分类: os] # [代码描述: List the content of the home directory] # [代码作者: Andy Breiner <breinera@gmail.com>] # [代码协议: GPL] # [SNIPPET_DOCS: http://docs.python.org/library/os.html, http://diveintopython.org/file_handling/os_module.html] import os # expand ~ to /home/ # also print out the content of the home directory as a list print os.listdir(os.path.expanduser("~")) # Loop over all the items and determine if they are a file or directory and # then print them out directory = os.path.expanduser("~") for f in os.listdir(directory): if os.path.isfile(os.path.join(directory, f)): print "File: " + f if os.path.isdir(os.path.join(directory, f)): print "Directory: " + f