# %% tekst = open('lem.txt', encoding='utf-8').readlines() # %% tekst # %% tekst = [] for wiersz in open('lem.txt', encoding='utf-8'): tekst.append(wiersz) # %% tekst = [wiersz.strip() for wiersz in open('lem.txt', encoding='utf-8')] tekst # %% plik = open('lem.txt', 'a', encoding='utf-8') print('\n\nKazimierz Wielki wpadł do butelki', file=plik) plik.close() # %% with open('lem.txt', 'a', encoding='utf-8') as plik: print('\n\n\n', file=plik) for _ in range(100): print('\nKazimierz Wielki wpadł do butelki', file=plik) # %% with open('lem.txt', mode='a', encoding='utf-8') as plik: plik.writelines(['Wanda, co nie chciała Niemca\n', 'Ale wolała Włocha']) # %% with open('lem.txt', 'r', encoding='utf-8' ) as plik: wiersz = plik.readline().strip() print(wiersz) print(wiersz.split()) # %% with open('adresy.txt', 'r', encoding='utf-8' ) as plik: tekst = plik.readlines() # usuń białe znaki tekst = [wiersz.strip() for wiersz in tekst] # usuń średniki metody = ('wycinek', 'replace') metoda = 'replace' if metoda == 'wycinek': tekst = [wiersz[:-1] for wiersz in tekst] elif metoda == 'replace': tekst = [wiersz.replace(';','') for wiersz in tekst] for i, mail in enumerate(tekst): prefiks, skrzynka = mail.split('@') print(f'Osoba numer {i+1} ma prefiks {prefiks} na skrzynce {skrzynka}') # %% for i in range(len(tekst)): prefiks, skrzynka = tekst[i].split('@') print(f'Osoba numer {i+1} ma prefiks {prefiks} na skrzynce {skrzynka}') # %% list(enumerate(tekst)) # %% import os os.getcwd() # %% sciezka = r"C:\Users\Nauczyciel\VS programy" print(sciezka) # %% os.listdir() # %% os.path.isdir('.venv'), os.path.isfile('.venv'), os.path.isfile('adresy.txt'), # %% list(os.scandir()) # %% [markdown] # #### Dlaczego `with` w tym miejscu, skoro nie ma z tego korzyści dla komputera? # # Bo **kod źródłowy jest dla człowieka, nie dla komputera** # # Dla człowieka takie użycie `with` jest podprogowym przekazem: wszystko co jest do zrobienia z katalogami ma być w obrębie tego fragmentu kodu, a nie gdzieś poza nim. # # Dobrą praktyką jest pisanie *kodów samokomentujących się* . # # Komentarze w kodzie mile widziane są tylko w książkach i artykułach demonstrujących użycie kodu, nie w prawdziwych projektach. # %% with os.scandir() as zawartosc_folder: for elem in zawartosc_folder: print(f'Katalog: {elem.name}') if elem.is_dir() else print(f'Plik: {elem.name}') # %% def relu(input): if input >= 0: return input else: return 0 # %% def relu(input): if input >= 0: return input return 0 # %% def relu(input): output = input if input >= 0 else 0 return output # %% '------'.join(tekst) # %%