Uke 3 #2 utkast Trygve

This commit is contained in:
Trygve 2023-09-28 14:59:11 +02:00
parent 27b4466948
commit 12530126dc

30
uke3.py
View File

@ -26,18 +26,28 @@ print(student_information("data.txt"))
#%% Task 2
import re
import pathlib import path
from pathlib import Path
def get_imp_file(file):
with open(file, 'r', encoding='utf-8') as f:
#lines = f.readlines()
imp=re.findall(r"import\s(\w+)", f.read())
return imp
def get_imp_dir(dir="./"):
txt = f.read()
# re.M gjør at ^ matcher starten av hver linje istedet for bare starten av stringen
ptr1 = re.compile(r"^import\s(\w+)", flags=re.M)
ptr2 = re.compile(r"^from\s(\w+)", flags=re.M)
imports = re.findall(ptr1, txt)
imports += re.findall(ptr2, txt)
get_imp_file("exercises2.py")
# vi filtrerer ut duplikater:
res = []
[res.append(x) for x in imports if x not in res]
return res
def print_imp_dir(path="./"):
p = Path(path)
files = list(p.glob('*.py'))
for f in files:
print(f'{Path.cwd()}+{f}: {get_imp_file(f)}')
print_imp_dir()
# %%