-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1
More file actions
30 lines (21 loc) · 839 Bytes
/
1
File metadata and controls
30 lines (21 loc) · 839 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/env python
import os
# example from http://www.doughellmann.com/PyMOTW/os/index.html#module-os
root = raw_input("Enter the root of the dirctory you want to walk> ")
#The function walk() traverses a directory recursively and for each directory
#generates a tuple containing the directory path, any immediate sub-directories
#of that path, and the names of any files in that directory.
for dir_name, sub_dirs, files in os.walk(root):
print '\n', dir_name
# list comprehension with subdirectories
sub_dirs = ['%s/' % n for n in sub_dirs ]
# Mix the directory contents together
contents = sub_dirs + files
contents.sort()
for c in contents:
print '\t%s' % c
# Another way of looking
for dir_name, sub_dirs, files in os.walk(root):
print dir_name
print sub_dirs
print files