-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumbering_remover.py
More file actions
45 lines (33 loc) · 1.21 KB
/
Copy pathnumbering_remover.py
File metadata and controls
45 lines (33 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
import re
from pathlib import Path
pythonFilesDir = Path("Al Sweigart/Al Sweigart Bigbook of small python Projects/source codes")
pattern = r'^\d+\.'
def removeNumbering(file):
"""Removes line numbering in a copy pasted python source code."""
with open(file, "r", encoding='utf-8') as inputFile:
outputList = []
for line in inputFile:
line = re.sub(pattern, '', line).lstrip(' ')
outputList.append(line)
return outputList
def isNumbered(file):
with open(file, "r", encoding='utf-8') as inputFile:
return any(re.match(pattern, line) for line in inputFile)
def writeData(file, data):
with open(file, "w", encoding='utf-8') as f:
f.writelines(data)
def main():
for file in pythonFilesDir.iterdir():
if (
not (file.name.startswith("numbering_remover") or file.name.startswith("project_file"))
and (file.is_file() and isNumbered(file))
):
try:
data = removeNumbering(file)
except Exception as e:
raise e
else:
writeData(file, data)
print(f"Removed numbering in {file.name}")
if __name__ == "__main__":
main()