Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8b7beb6

Browse files
committed
Use os.walk() to find files to delete.
1 parent 2f36b3e commit 8b7beb6

1 file changed

Lines changed: 14 additions & 13 deletions

File tree

PCbuild/rmpyc.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
# Remove all the .pyc and .pyo files under ../Lib.
22

3+
34
def deltree(root):
45
import os
5-
def rm(path):
6-
os.unlink(path)
6+
from os.path import join
7+
78
npyc = npyo = 0
8-
dirs = [root]
9-
while dirs:
10-
dir = dirs.pop()
11-
for short in os.listdir(dir):
12-
full = os.path.join(dir, short)
13-
if os.path.isdir(full):
14-
dirs.append(full)
15-
elif short.endswith(".pyc"):
9+
for root, dirs, files in os.walk(root):
10+
for name in files:
11+
delete = False
12+
if name.endswith('.pyc'):
13+
delete = True
1614
npyc += 1
17-
rm(full)
18-
elif short.endswith(".pyo"):
15+
elif name.endswith('.pyo'):
16+
delete = True
1917
npyo += 1
20-
rm(full)
18+
19+
if delete:
20+
os.remove(join(root, name))
21+
2122
return npyc, npyo
2223

2324
npyc, npyo = deltree("../Lib")

0 commit comments

Comments
 (0)