You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 7.-input-and-output.md
+8-16
Original file line number
Diff line number
Diff 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ư
223
223
>>> f.read()
224
224
''
225
225
```
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.>>>
228
227
229
228
```text
230
229
>>> 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ư
234
233
>>> f.readline()
235
234
''
236
235
```
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.>>>
239
237
240
238
```text
241
239
>>> for line in f:
@@ -244,28 +242,23 @@ For reading lines from a file, you can loop over the file object. This is memory
244
242
This is the first line of the file.
245
243
Second line of the file
246
244
```
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.>>>
251
247
252
248
```text
253
249
>>> f.write('This is a test\n')
254
250
15
255
251
```
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:>>>
256
253
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
260
255
>>> value = ('the answer', 42)
261
256
>>> s = str(value) # convert the tuple to string
262
257
>>> f.write(s)
263
258
18
264
259
```
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.>>>
269
262
270
263
```text
271
264
>>> f = open('workfile', 'rb+')
@@ -280,7 +273,6 @@ b'5'
280
273
>>> f.read(1)
281
274
b'd'
282
275
```
283
-
284
276
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.
285
277
286
278
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