-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevel14.py
More file actions
35 lines (24 loc) · 890 Bytes
/
level14.py
File metadata and controls
35 lines (24 loc) · 890 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
34
35
# 18. Pemrosesan File di Python
# Python memungkinkan kita untuk membaca, menulis, dan memanipulasi file dengan sangat mudah.
# 1. Membuka dan Membaca File
# Gunakan fungsi open() untuk membuka file.
# Mode yang sering digunakan:
# 'r': Baca file (default).
# 'w': Tulis file (overwrite).
# 'a': Tulis file (append).
# 'b': Mode biner (untuk file gambar, PDF, dll.).
with open('contoh.txt', 'r') as file:
data = file.read()
print(data)
# Menulis ke file (overwrite)
with open('contoh.txt', 'w') as file:
file.write("Ini adalah baris pertama.\n")
# Menambah isi ke file
with open('contoh.txt', 'a') as file:
file.write("Ini adalah baris kedua.\n")
with open('contoh.txt', 'r') as file:
data = file.read()
print(data)
with open('contoh.txt', 'r') as file:
for baris in file:
print(baris.strip()) # .strip() untuk menghapus karakter newline