Python juga mendukung penanganan file dan memungkinkan pengguna untuk menangani file i. e. , untuk membaca dan menulis file, bersama dengan banyak opsi penanganan file lainnya, untuk mengoperasikan file. Konsep penanganan file telah diperluas ke berbagai bahasa lain, tetapi penerapannya rumit atau panjang, tetapi seperti konsep Python lainnya, konsep ini juga mudah dan singkat. Python memperlakukan file secara berbeda sebagai teks atau biner dan ini penting. Setiap baris kode menyertakan urutan karakter dan membentuk file teks. Setiap baris file diakhiri dengan karakter khusus, yang disebut karakter EOL atau End of Line seperti koma {,} atau karakter baris baru. Itu mengakhiri baris saat ini dan memberi tahu juru bahasa bahwa yang baru telah dimulai. Mari kita mulai dengan membaca dan menulis file.
Bekerja dari fungsi open()
Sebelum melakukan operasi apa pun pada file seperti membaca atau menulis, pertama-tama kita harus membuka file itu. Untuk ini, kita harus menggunakan fungsi bawaan Python open() tetapi pada saat pembukaan, kita harus menentukan mode, yang mewakili tujuan dari file pembuka
f = open(filename, mode)Di mana mode berikut didukung
- r. buka file yang ada untuk operasi baca
- w. buka file yang ada untuk operasi tulis. Jika file sudah berisi beberapa data maka itu akan ditimpa tetapi jika file tersebut tidak ada maka itu akan membuat file juga
- a. buka file yang ada untuk menambahkan operasi. Itu tidak akan menimpa data yang ada
- r+. Untuk membaca dan menulis data ke dalam file. Data sebelumnya dalam file akan diganti
- w+. Untuk menulis dan membaca data. Ini akan menimpa data yang ada
- +. Untuk menambahkan dan membaca data dari file. Itu tidak akan menimpa data yang ada
Lihatlah contoh di bawah ini
Python3
# a file named "geek", will be opened with the reading mode.
file = open('geek.txt','r'rstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side.0
rstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side._1
rstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side.2 rstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side.3rstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side.4 filerstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side.6
rstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side.7rstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side.8 rstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side.9
Perintah open akan membuka file dalam mode read dan loop for akan mencetak setiap baris yang ada dalam file.
Bekerja dari read() mode
Ada lebih dari satu cara untuk membaca file dengan Python. Jika Anda perlu mengekstrak string yang berisi semua karakter dalam file, maka kita dapat menggunakan file. Baca(). Kode lengkap akan bekerja seperti ini.
Python3
# a file named "geek", will be opened with the reading mode.0
file = open(# a file named "geek", will be opened with the reading mode.5,# a file named "geek", will be opened with the reading mode.7rstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side.0
rstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side.8 (filefile2
Cara lain untuk membaca file adalah dengan memanggil sejumlah karakter tertentu seperti pada kode berikut, interpreter akan membaca lima karakter pertama dari data yang disimpan dan mengembalikannya sebagai string.
Python3
file_3
file = open(# a file named "geek", will be opened with the reading mode.5,# a file named "geek", will be opened with the reading mode.7rstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side.0
rstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side.8 (file=5=6=7
Membuat file menggunakan mode write()
Mari kita lihat cara membuat file dan cara kerja mode tulis, jadi untuk memanipulasi file, tulis berikut ini di lingkungan Python Anda.
Python3
=8
file = open('geek.txt',open5rstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side.0
fileopen8open9rstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side.0
fileopen8(3rstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side.0
file(6
Perintah close() menghentikan semua sumber daya yang digunakan dan membebaskan sistem dari program khusus ini.
Bekerja dari append() mode
Mari kita lihat bagaimana mode append bekerja.
Python3
(7
file = open('geek.txt','geek.txt'4rstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side.0
fileopen8'geek.txt'8rstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side.0
file(6
Ada juga berbagai perintah lain dalam penanganan file yang digunakan untuk menangani berbagai tugas seperti.
rstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side._Ini dirancang untuk memberikan sintaks yang jauh lebih bersih dan penanganan pengecualian saat Anda bekerja dengan kode. Itu menjelaskan mengapa praktik yang baik untuk menggunakannya dengan pernyataan jika berlaku. Ini sangat membantu karena dengan menggunakan metode ini, setiap file yang dibuka akan ditutup secara otomatis setelah selesai, jadi pembersihan otomatis.
Contoh.
Python3
,_2
,3open(# a file named "geek", will be opened with the reading mode.5,7________44__49_______9
rstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side.7'r'1= file'r'4
'r'5
Menggunakan tulis bersama dengan fungsi with()
Kita juga dapat menggunakan fungsi tulis bersama dengan fungsi with().
Python3
'r'6
,3open(# a file named "geek", will be opened with the reading mode.5,rstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side.02rstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side.03
rstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side.7rstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side.05rstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side.06rstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side.0
split() menggunakan penanganan file
Kami juga dapat membagi garis menggunakan penanganan file dengan Python. Ini membagi variabel ketika ruang ditemui. Kalian juga bisa melakukan split menggunakan karakter apapun sesuai keinginan kita. Ini kodenya
Python3
rstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side._08
,3________46__47_______rstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side.12______49_______# a file named "geek", will be opened with the reading mode.7,7filerstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side.6
rstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side.7'r'1= filerstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side.22
rstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side.7rstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side.2 rstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side.25rstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side.4 rstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side.27
rstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side.28rstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side.29= rstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side.31
rstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side.28rstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side.8 rstrip(): This function strips each line of a file off spaces from the right-hand side. lstrip(): This function strips each line of a file off spaces from the left-hand side.34
Ada juga berbagai fungsi lain yang membantu memanipulasi file dan isinya. Seseorang dapat menjelajahi berbagai fungsi lain di Python Docs
Artikel ini disumbangkan oleh Chinmoy Lenka. Jika Anda menyukai GeeksforGeeks dan ingin berkontribusi, Anda juga dapat menulis artikel menggunakan tulis. geeksforgeeks. org atau kirimkan artikel Anda ke review-team@geeksforgeeks. org. Lihat artikel Anda muncul di halaman utama GeeksforGeeks dan bantu Geeks lainnya
Silakan tulis komentar jika Anda menemukan sesuatu yang salah, atau jika Anda ingin berbagi informasi lebih lanjut tentang topik yang dibahas di atas.