-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.py
More file actions
33 lines (26 loc) · 831 Bytes
/
file.py
File metadata and controls
33 lines (26 loc) · 831 Bytes
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
#### DOSYA OKUMA ####
# Eğer dosya yoksa
try:
file = open("info.txt", "r")
except FileNotFoundError:
print("Dosya Bulunamadı")
# file üzerinde satır satır gezme
for i in file:
print(i)
file.close()
# Tüm satırları listeye alma
liste = file.readlines()
file.close()
# Dosya okumanın kolay yolu open ve close fonksiyonlarını kullanamıza gerek kalmaz.
# İlk 4 karakterin okuma, boş bırakılırsa tamamını okur.
with open(example1, "r") as file1:
print(file1.read(4))
#### DOSYA YAZMA ####
# Satır yazma
with open('/resources/data/Example2.txt', 'w') as writefile:
writefile.write("This is line A")
#### DOSYA KOPYALAMA ####
with open('Example2.txt','r') as readfile:
with open('Example3.txt','w') as writefile:
for line in readfile:
writefile.write(line)