-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchFiles_Recursion.py
More file actions
61 lines (41 loc) · 1.21 KB
/
SearchFiles_Recursion.py
File metadata and controls
61 lines (41 loc) · 1.21 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# coding=UTF-8
import os, sys
from os.path import join, isfile, isdir
#Official os.walk()
def originOsWalk(path):
for root, dirs, files in os.walk(path):
for f in files:
print("file:"+join(root, f))
for d in dirs:
print("directory:"+join(root, d))
#Practice a simple version: os.walk
def ScanDir(path):
files = os.listdir(path)
for f in files:
abspath=join(path, f)
SearchFile(abspath)
def SearchFile(abspath):
if isfile(abspath):
print("file:", abspath)
elif isdir(abspath):
print("directory:", abspath)
ScanDir(abspath)
#Run above of two functions
def Run(num, OriPath):
#https://docs.python.org/2/faq/design.html#why-isn-t-there-a-switch-or-case-statement-in-python
functions = {
'a': originOsWalk,
'b': ScanDir
}
func = functions.get(num,'invalid number')
func(OriPath)
OriPath = r"C:\Users\yug\Downloads"
if len(sys.argv)>1:
num = sys.argv[1]
Run(num, OriPath)
'''
python SearchFiles_Recursion.py a
: originOsWalk
python SearchFiles_Recursion.py b
: ScanDir
'''