Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7251ea2

Browse files
authored
Update 7.-input-and-output.md
1 parent 7f2fff8 commit 7251ea2

File tree

1 file changed

+8
-16
lines changed

1 file changed

+8
-16
lines changed

7.-input-and-output.md

+8-16
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,7 @@ Các ví dụ còn lại trong phần này giả định rằng một đối tư
223223
>>> f.read()
224224
''
225225
```
226-
227-
`f.readline()` reads a single line from the file; a newline character \(`\n`\) is left at the end of the string, and is only omitted on the last line of the file if the file doesn’t end in a newline. This makes the return value unambiguous; if `f.readline()` returns an empty string, the end of the file has been reached, while a blank line is represented by `'\n'`, a string containing only a single newline.>>>
226+
Hàm `f.readline()` đọc từng dòng trong file; Ký tự xuống dòng \(`\n`\) được thêm vào cuối chuỗi, và chỉ được bỏ qua khi đọc dòng cuối cùng của file nếu file đó không kết thúc bằng ký tự xuống dòng. Điều này làm cho giá trị trả về không rõ ràng; nếu hàm `f.readline()` trả về một chuỗi rỗng, mà đọc tới dòng cuối file, trong khi dòng trống thể hiện bằng `'\n'`, thì một chuỗi bao gồm chỉ một ký tự xuống dòng.>>>
228227

229228
```text
230229
>>> f.readline()
@@ -234,8 +233,7 @@ Các ví dụ còn lại trong phần này giả định rằng một đối tư
234233
>>> f.readline()
235234
''
236235
```
237-
238-
For reading lines from a file, you can loop over the file object. This is memory efficient, fast, and leads to simple code:>>>
236+
Vì đọc các dòng trong file, nên có thể dùng vòng lặp trên đối tượng file. Đây là một cách đơn giản và hiệu quả để đọc từng dòng của file.>>>
239237

240238
```text
241239
>>> for line in f:
@@ -244,28 +242,23 @@ For reading lines from a file, you can loop over the file object. This is memory
244242
This is the first line of the file.
245243
Second line of the file
246244
```
247-
248-
If you want to read all the lines of a file in a list you can also use `list(f)` or `f.readlines()`.
249-
250-
`f.write(string)` writes the contents of _string_ to the file, returning the number of characters written.>>>
245+
Nếu muốn đọc tất cả các dòng của file và đưa vào 1 list, có thể sử dụng `list(f)` hoặc `f.readlines()`.
246+
Hàm `f.write(string)` ghi nội dung của chuỗi (_string_) vào file, trả về số lượng ký tự được ghi.>>>
251247

252248
```text
253249
>>> f.write('This is a test\n')
254250
15
255251
```
252+
Những kiểu đối tượng khác cần phải được chuyển đổi - hoặc là sang 1 chuỗi (string) \(trong chế độ văn bản - text mode\) hoặc là đối tượng byte \(trong chế độ nhị phân - binar\) - trước khi ghi ra file:>>>
256253

257-
Other types of objects need to be converted – either to a string \(in text mode\) or a bytes object \(in binary mode\) – before writing them:>>>
258-
259-
```text
254+
```textode
260255
>>> value = ('the answer', 42)
261256
>>> s = str(value) # convert the tuple to string
262257
>>> f.write(s)
263258
18
264259
```
265-
266-
`f.tell()` returns an integer giving the file object’s current position in the file represented as number of bytes from the beginning of the file when in binary mode and an opaque number when in text mode.
267-
268-
To change the file object’s position, use `f.seek(offset, from_what)`. The position is computed from adding _offset_ to a reference point; the reference point is selected by the _from\_what_ argument. A _from\_what_ value of 0 measures from the beginning of the file, 1 uses the current file position, and 2 uses the end of the file as the reference point. _from\_what_ can be omitted and defaults to 0, using the beginning of the file as the reference point.>>>
260+
Hàm `f.tell()` trả về một số nguyên, số nguyên này định vị trí hiện tại của đối tượng file trong file hiển thị số lượng byte từ đầu file khi ở chế độ nhị phân (binary mode) và một số opaque number ở chế độ văn bản (text mode)
261+
Để thay đổi vị trí của đối tượng file, sử dụng hàm `f.seek(offset, from_what)`. Vị trí được tính toán dựa trên tham số _offset_; điểm tham chiếu được lựa chọn dựa vào tham số _from\_what_. N _from\_what_ là 0, vị trí tham chiếu từ đầu file, _from\_what_ là 1, ví trí tham chiếu là vị trí file hiện tại. _from\_what_ là 2, vị trí tham chiếu là vị trí cuối của tệp.>>>
269262

270263
```text
271264
>>> f = open('workfile', 'rb+')
@@ -280,7 +273,6 @@ b'5'
280273
>>> f.read(1)
281274
b'd'
282275
```
283-
284276
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.
285277

286278
File objects have some additional methods, such as `isatty()` and `truncate()` which are less frequently used; consult the Library Reference for a complete guide to file objects.

0 commit comments

Comments
 (0)