main.py
· 2.8 KiB · Python
Raw
# %%
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 <font color="green"> **kod źródłowy jest dla człowieka, nie dla komputera** </font>
#
# 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 <font color="green"> *kodów samokomentujących się* </font>.
#
# 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)
# %%
| 1 | # %% |
| 2 | tekst = open('lem.txt', encoding='utf-8').readlines() |
| 3 | |
| 4 | # %% |
| 5 | tekst |
| 6 | |
| 7 | # %% |
| 8 | tekst = [] |
| 9 | for wiersz in open('lem.txt', encoding='utf-8'): |
| 10 | tekst.append(wiersz) |
| 11 | |
| 12 | # %% |
| 13 | tekst = [wiersz.strip() for wiersz in open('lem.txt', encoding='utf-8')] |
| 14 | tekst |
| 15 | |
| 16 | # %% |
| 17 | plik = open('lem.txt', 'a', encoding='utf-8') |
| 18 | print('\n\nKazimierz Wielki wpadł do butelki', file=plik) |
| 19 | plik.close() |
| 20 | |
| 21 | # %% |
| 22 | with open('lem.txt', 'a', encoding='utf-8') as plik: |
| 23 | print('\n\n\n', file=plik) |
| 24 | for _ in range(100): |
| 25 | print('\nKazimierz Wielki wpadł do butelki', file=plik) |
| 26 | |
| 27 | # %% |
| 28 | with open('lem.txt', mode='a', encoding='utf-8') as plik: |
| 29 | plik.writelines(['Wanda, co nie chciała Niemca\n', 'Ale wolała Włocha']) |
| 30 | |
| 31 | # %% |
| 32 | with open('lem.txt', 'r', encoding='utf-8' ) as plik: |
| 33 | wiersz = plik.readline().strip() |
| 34 | |
| 35 | print(wiersz) |
| 36 | print(wiersz.split()) |
| 37 | |
| 38 | # %% |
| 39 | with open('adresy.txt', 'r', encoding='utf-8' ) as plik: |
| 40 | tekst = plik.readlines() |
| 41 | |
| 42 | # usuń białe znaki |
| 43 | tekst = [wiersz.strip() for wiersz in tekst] |
| 44 | |
| 45 | # usuń średniki |
| 46 | metody = ('wycinek', 'replace') |
| 47 | metoda = 'replace' |
| 48 | if metoda == 'wycinek': |
| 49 | tekst = [wiersz[:-1] for wiersz in tekst] |
| 50 | elif metoda == 'replace': |
| 51 | tekst = [wiersz.replace(';','') for wiersz in tekst] |
| 52 | |
| 53 | |
| 54 | |
| 55 | for i, mail in enumerate(tekst): |
| 56 | prefiks, skrzynka = mail.split('@') |
| 57 | print(f'Osoba numer {i+1} ma prefiks {prefiks} na skrzynce {skrzynka}') |
| 58 | |
| 59 | |
| 60 | |
| 61 | # %% |
| 62 | for i in range(len(tekst)): |
| 63 | prefiks, skrzynka = tekst[i].split('@') |
| 64 | print(f'Osoba numer {i+1} ma prefiks {prefiks} na skrzynce {skrzynka}') |
| 65 | |
| 66 | # %% |
| 67 | list(enumerate(tekst)) |
| 68 | |
| 69 | # %% |
| 70 | import os |
| 71 | os.getcwd() |
| 72 | |
| 73 | # %% |
| 74 | sciezka = r"C:\Users\Nauczyciel\VS programy" |
| 75 | print(sciezka) |
| 76 | |
| 77 | # %% |
| 78 | os.listdir() |
| 79 | |
| 80 | # %% |
| 81 | os.path.isdir('.venv'), os.path.isfile('.venv'), os.path.isfile('adresy.txt'), |
| 82 | |
| 83 | # %% |
| 84 | list(os.scandir()) |
| 85 | |
| 86 | # %% [markdown] |
| 87 | # #### Dlaczego `with` w tym miejscu, skoro nie ma z tego korzyści dla komputera? |
| 88 | # |
| 89 | # Bo <font color="green"> **kod źródłowy jest dla człowieka, nie dla komputera** </font> |
| 90 | # |
| 91 | # 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. |
| 92 | # |
| 93 | # Dobrą praktyką jest pisanie <font color="green"> *kodów samokomentujących się* </font>. |
| 94 | # |
| 95 | # Komentarze w kodzie mile widziane są tylko w książkach i artykułach demonstrujących użycie kodu, nie w prawdziwych projektach. |
| 96 | |
| 97 | # %% |
| 98 | with os.scandir() as zawartosc_folder: |
| 99 | for elem in zawartosc_folder: |
| 100 | print(f'Katalog: {elem.name}') if elem.is_dir() else print(f'Plik: {elem.name}') |
| 101 | |
| 102 | |
| 103 | # %% |
| 104 | def relu(input): |
| 105 | if input >= 0: |
| 106 | return input |
| 107 | else: |
| 108 | return 0 |
| 109 | |
| 110 | # %% |
| 111 | def relu(input): |
| 112 | if input >= 0: |
| 113 | return input |
| 114 | return 0 |
| 115 | |
| 116 | # %% |
| 117 | def relu(input): |
| 118 | |
| 119 | output = input if input >= 0 else 0 |
| 120 | return output |
| 121 | |
| 122 | # %% |
| 123 | '------'.join(tekst) |
| 124 | |
| 125 | # %% |
| 126 | |
| 127 | |
| 128 | |
| 129 |