Penanganan file dalam pertanyaan python

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

Baca Penanganan File Python untuk menyelesaikan kuis ini

  • Kuis ini berisi 12 Pertanyaan. Selesaikan 8 dengan benar untuk lulus ujian
  • Anda harus membaca semua jawaban yang diberikan dan mengklik jawaban yang benar

1. Pilih semua metode yang benar untuk menghapus file dengan Python

  os.remove('file_path')_

  os.rmfile('file_path')_

  pathlib.Path('file_path').remove()_

  os.unlink('file_path')_

2. Pilih metode yang benar untuk menulis daftar baris ke file

  write(list)_

  writelines(list)_

  writelist(list)_

3. Pilih output yang benar dari kode berikut

fp.seek(5, 1)

  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

  read(1)_

  readlines(1)_

  os.rmfile('file_path')0

  os.rmfile('file_path')1

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

  os.rmfile('file_path')2

  os.rmfile('file_path')3

  os.rmfile('file_path')4

  os.rmfile('file_path')5

7. Pilih semua pernyataan benar saat file dibuka menggunakan pernyataan os.rmfile('file_path')6

  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

  1. r. buka file yang ada untuk operasi baca
  2. 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
  3. a. buka file yang ada untuk menambahkan operasi. Itu tidak akan menimpa data yang ada
  4. r+. Untuk membaca dan menulis data ke dalam file. Data sebelumnya dalam file akan diganti
  5. w+. Untuk menulis dan membaca data. Ini akan menimpa data yang ada
  6. +. 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',________67__1_______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.
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() 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.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.
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.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.
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=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',open5

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

fileopen8open9

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

fileopen8(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.
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'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.
0

fileopen8'geek.txt'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.
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________66______7file,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.
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 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

________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.