Kuis penanganan file Python ini menyediakan pertanyaan pilihan ganda (MCQ) untuk membiasakan diri dengan operasi file Python. Kuis ini menguji pengetahuan Anda tentang operasi file seperti membuka file, membaca file, menulis file, menutupnya, mengganti nama file, menghapus file, dan berbagai metode file Show
Baca Penanganan File Python untuk menyelesaikan kuis ini
1. Pilih semua metode yang benar untuk menghapus file dengan Python 2. Pilih metode yang benar untuk menulis daftar baris ke file 3. Pilih output yang benar dari kode berikut
Pindahkan penunjuk file lima karakter ke depan dari posisi saat ini. Pindahkan penunjuk file lima karakter ke depan dari awal file. Pindahkan penunjuk file lima karakter ke belakang dari posisi saat ini. Pindahkan penunjuk file lima karakter ke depan dari akhir file. 4. Metode mana yang digunakan untuk membaca file baris demi baris 5. Jika file dibuka dalam mode tulis dan sudah ada, ini akan memotong konten yang ada dan menempatkan filehandle di awal file. Benar Salah 6. Pilih semua metode yang benar untuk menyalin konten file sumber ke file tujuan 7. Pilih semua pernyataan benar saat file dibuka menggunakan pernyataan Pernyataan with menyederhanakan penanganan pengecualian File ditutup secara otomatis setelah keluar dari blok, dan semua sumber daya yang terikat dengan file dilepaskan. 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
Lihatlah contoh di bawah ini 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._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.3 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.4 file 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.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 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 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() modeAda 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
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.8 ( file file 2Cara 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
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.8 ________64__61_______ = 5= 6= 7Membuat 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
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.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.0
Perintah close() menghentikan semua sumber daya yang digunakan dan membebaskan sistem dari program khusus ini. Bekerja dari append() modeMari kita lihat bagaimana mode append bekerja. 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.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.0
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
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
Menggunakan tulis bersama dengan fungsi with()Kita juga dapat menggunakan fungsi tulis bersama dengan fungsi with(). 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.02 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.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.7 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.05 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.06 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 split() menggunakan penanganan fileKami 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 Python3rstrip(): 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 ________66______3________63__64_______ 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______66_______ # a file named "geek", will be opened with the reading mode. 7, 7file 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.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= file 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.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.7 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.25 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.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.28 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.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.28 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 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. Apa penanganan file dengan Python dengan contoh?Penanganan file merupakan bagian integral dari pemrograman. Penanganan file di Python disederhanakan dengan metode bawaan, yang meliputi membuat, membuka, dan menutup file . Saat file terbuka, Python juga memungkinkan melakukan berbagai operasi file, seperti membaca, menulis, dan menambahkan informasi.
Apa metode dalam penanganan file dengan Python?Pelajari lebih lanjut tentang objek file di Tutorial Penanganan File Python kami.
. Metode File Python Bagaimana Anda menulis penanganan file dengan Python?Tulis File Python . ❮ Sebelumnya Berikutnya ❯ Contoh. Buka file "demofile2. txt" dan tambahkan konten ke file. f = buka("demofile2. txt", "a") f. write("Sekarang file memiliki lebih banyak konten. ") f. menutup(). Contoh. Buka file "demofile3. txt" dan menimpa konten. f = buka("demofile3. txt", "w") f. tulis("Waduh. . ❮ Sebelumnya Berikutnya ❯ Pustaka mana yang digunakan untuk penanganan file dengan Python?pathlib pertama kali diperkenalkan di Python 3. 4 dan merupakan tambahan yang bagus untuk Python yang menyediakan antarmuka berorientasi objek ke sistem file. Pada contoh di atas, Anda memanggil pathlib. |