Pelokalan
Python ID
id en
Masuk
Proyek
tutorial__inputoutput.po Berkas proyek · python-docs-id-3.14 · Bahasa Indonesia
Modul PO Hanya lihat
Teks sumber — English
Terjemahan — Bahasa Indonesia
To use :ref:`formatted string literals <tut-f-strings>`, begin a string with ``f`` or ``F`` before the opening quotation mark or triple quotation mark. Inside this string, you can write a Python expression between ``{`` and ``}`` characters that can refer to variables or literal values.
Untuk menggunakan :ref:`formatted string literals <tut-f-strings>`, mulailah string dengan ``f`` atau ``F`` sebelum tanda kutip pembuka atau tanda kutip tiga. Di dalam string ini, Anda bisa menulis ekspresi Python antara karakter ``{`` dan ``}`` yang dapat merujuk ke variabel atau nilai literal.
:func:`open` returns a :term:`file object`, and is most commonly used with two positional arguments: ``open(filename, mode)``
>>> f = open('workfile', 'w')
The first argument is a string containing the filename. The second argument is another string containing a few characters describing the way in which the file will be used. *mode* can be ``'r'`` when the file will only be read, ``'w'`` for only writing (an existing file with the same name will be erased), and ``'a'`` opens the file for appending; any data written to the file is automatically added to the end. ``'r+'`` opens the file for both reading and writing. The *mode* argument is optional; ``'r'`` will be assumed if it's omitted.
Argumen pertama adalah string yang berisi nama file. Argumen kedua adalah string lain yang berisi beberapa karakter yang menggambarkan cara berkas akan digunakan. *mode* dapat ``'r'`` ketika file hanya akan dibaca, ``'w'`` untuk hanya menulis (berkas yang ada dengan nama yang sama akan dihapus), dan ``'a'`` membuka berkas untuk ditambahkan; setiap data yang ditulis ke file secara otomatis ditambahkan ke bagian akhir. ``'r+'`` membuka berkas untuk membaca dan menulis. Argumen *mode* adalah opsional; ``'r'`` akan diasumsikan jika dihilangkan.
Normally, files are opened in :dfn:`text mode`, that means, you read and write strings from and to the file, which are encoded in a specific *encoding*. If *encoding* is not specified, the default is UTF-8 (see :func:`open`). Appending a ``'b'`` to the mode opens the file in :dfn:`binary mode`. Binary mode data is read and written as :class:`bytes` objects. You can not specify *encoding* when opening file in binary mode.
>>> with open('workfile') as f: ... read_data = f.read() >>> # We can check that the file has been automatically closed. >>> f.closed True
>>> f = open('workfile', 'rb+') >>> f.write(b'0123456789abcdef') 16 >>> f.seek(5) # Go to the 6th byte in the file 5 >>> f.read(1) b'5' >>> f.seek(-3, 2) # Go to the 3rd byte before the end 13 >>> f.read(1) b'd'
In text files (those opened without a ``b`` in the mode string), only seeks relative to the beginning of the file are allowed (the exception being seeking to the very file end with ``seek(0, 2)``) and the only valid *offset* values are those returned from the ``f.tell()``, or zero. Any other *offset* value produces undefined behaviour.
Dalam file teks (yang dibuka tanpa ``b`` dalam mode string), hanya mencari relatif ke awal file yang diizinkan (pengecualian sedang mencari sampai akhir file dengan ``seek(0, 2)``) dan satu-satunya nilai *offset* yang valid adalah yang dikembalikan dari ``f.tell()``, atau nol. Nilai *offset* lainnya menghasilkan perilaku tidak terdefinisi.
Another variant of the :func:`~json.dumps` function, called :func:`~json.dump`, simply serializes the object to a :term:`text file`. So if ``f`` is a :term:`text file` object opened for writing, we can do this::
Varian lain dari fungsi :func:`~json.dumps`, disebut :func:`~json.dump`, dengan mudah membuat serialisasi objek menjadi :term: `text file`. Jadi jika ``f`` adalah objek :term:`text file` dibuka untuk menulis, kita dapat melakukan ini::
To decode the object again, if ``f`` is a :term:`binary file` or :term:`text file` object which has been opened for reading::
open
buka