import oscount = 0path = r'C:\Users\xxxx\Desktop\top'for root, dirs, files in os.walk(path): #print files fileLength = len(files) if fileLength != 0: count = count + fileLengthprint "The number of files under <%s> is: %d" %(path,count)
上面是程序是 统计 C:\Users\xxxx\Desktop\top 目录下文件的个数 使用的时候 将 path 替换为想要的就行了 注意 path 等号 后面的 ‘r’