Hướng dẫn write text file python - viết tập tin văn bản python

Tóm tắt: Trong hướng dẫn này, bạn sẽ học cách tạo một tệp văn bản mới trong Python bằng hàm

with open['docs/readme.txt', 'w'] as f: f.write['Create a new text file!']

Code language: Python [python]
1.: in this tutorial, you’ll learn how to create a new text file in Python using the

with open['docs/readme.txt', 'w'] as f: f.write['Create a new text file!']

Code language: Python [python]
1 function.

Sử dụng hàm Open [] để tạo tệp văn bản mới

Để tạo một tệp văn bản mới, bạn sử dụng hàm

with open['docs/readme.txt', 'w'] as f: f.write['Create a new text file!']

Code language: Python [python]
1. Hàm

with open['docs/readme.txt', 'w'] as f: f.write['Create a new text file!']

Code language: Python [python]
1 có nhiều tham số. Tuy nhiên, chúng tôi sẽ tập trung vào hai tham số đầu tiên:

f = open[path_to_file, mode]

Trong cú pháp này, tham số

with open['docs/readme.txt', 'w'] as f: f.write['Create a new text file!']

Code language: Python [python]
4 chỉ định đường dẫn đến tệp văn bản mà bạn muốn tạo.

Để tạo tệp văn bản mới, bạn sử dụng một trong các chế độ sau:

  • with open['docs/readme.txt', 'w'] as f: f.write['Create a new text file!']

    Code language: Python [python]
    5 - Mở một tệp để viết. Nếu tệp không tồn tại, hàm

    with open['docs/readme.txt', 'w'] as f: f.write['Create a new text file!']

    Code language: Python [python]
    1 sẽ tạo một tệp mới. Mặt khác, nó sẽ ghi đè nội dung của tệp hiện có.
  • with open['docs/readme.txt', 'w'] as f: f.write['Create a new text file!']

    Code language: Python [python]
    7 - ​​Mở một tệp để tạo độc quyền. Nếu tệp tồn tại, hàm

    with open['docs/readme.txt', 'w'] as f: f.write['Create a new text file!']

    Code language: Python [python]
    1 sẽ gây ra lỗi [

    with open['docs/readme.txt', 'w'] as f: f.write['Create a new text file!']

    Code language: Python [python]
    9]. Nếu không, nó sẽ tạo tệp văn bản.

Ví dụ: sau đây tạo ra một tệp mới có tên

FileNotFoundError: [Errno 2] No such file or directory: 'docs/readme.txt'

Code language: JavaScript [javascript]
0 và viết một số văn bản vào đó:

with open['readme.txt', 'w'] as f: f.write['Create a new text file!']

Code language: JavaScript [javascript]

Tập lệnh này tạo một tệp có tên

FileNotFoundError: [Errno 2] No such file or directory: 'docs/readme.txt'

Code language: JavaScript [javascript]
0 trong cùng một thư mục trong đó tệp tập lệnh định vị. Nếu bạn muốn tạo một tệp trong một thư mục được chỉ định, ví dụ:

FileNotFoundError: [Errno 2] No such file or directory: 'docs/readme.txt'

Code language: JavaScript [javascript]
2, bạn cần đảm bảo rằng thư mục

FileNotFoundError: [Errno 2] No such file or directory: 'docs/readme.txt'

Code language: JavaScript [javascript]
3 tồn tại trước khi tạo tệp. Nếu không, bạn sẽ nhận được một ngoại lệ. Ví dụ:

with open['docs/readme.txt', 'w'] as f: f.write['Create a new text file!']

Code language: Python [python]

Error:

FileNotFoundError: [Errno 2] No such file or directory: 'docs/readme.txt'

Code language: JavaScript [javascript]

Trong ví dụ này, Python nêu ra một ngoại lệ vì thư mục

FileNotFoundError: [Errno 2] No such file or directory: 'docs/readme.txt'

Code language: JavaScript [javascript]
3 không tồn tại. Do đó, nó không thể tạo tệp

FileNotFoundError: [Errno 2] No such file or directory: 'docs/readme.txt'

Code language: JavaScript [javascript]
0 trong thư mục đó. Để khắc phục sự cố, trước tiên bạn cần tạo thư mục

FileNotFoundError: [Errno 2] No such file or directory: 'docs/readme.txt'

Code language: JavaScript [javascript]
3 và sau đó tạo tệp

FileNotFoundError: [Errno 2] No such file or directory: 'docs/readme.txt'

Code language: JavaScript [javascript]
0 trong thư mục đó.

Ngoài ra, bạn có thể xử lý ngoại lệ bằng cách sử dụng câu lệnh Try-Except như sau:

try: with open['docs/readme.txt', 'w'] as f: f.write['Create a new text file!'] except FileNotFoundError: print["The 'docs' directory does not exist"]

Code language: Python [python]

Output:

The 'docs' directory does not exist

Code language: plaintext [plaintext]

Nếu bạn không muốn tạo một tệp văn bản mới trong trường hợp nó đã tồn tại, bạn có thể sử dụng chế độ

with open['docs/readme.txt', 'w'] as f: f.write['Create a new text file!']

Code language: Python [python]
7 khi gọi hàm

with open['docs/readme.txt', 'w'] as f: f.write['Create a new text file!']

Code language: Python [python]
1:

with open['readme.txt', 'x'] as f: f.write['Create a new text file!']

Code language: Python [python]

Bản tóm tắt

  • Sử dụng chức năng

    with open['docs/readme.txt', 'w'] as f: f.write['Create a new text file!']

    Code language: Python [python]
    1 với chế độ

    with open['docs/readme.txt', 'w'] as f: f.write['Create a new text file!']

    Code language: Python [python]
    5 hoặc

    with open['docs/readme.txt', 'w'] as f: f.write['Create a new text file!']

    Code language: Python [python]
    7 để tạo tệp văn bản mới.

Bạn có thấy hướng dẫn này hữu ích không?

Python Cung Cấp Các chức năng cơ bản và phương thức cần thiết để Thao Tácc Các. BÀi VIếT NÀY TUY

1. Tệp Mở

Trước Khil lào việc với bất cứ tập tin noro, bạn phải mở file ĐÓ. Để Mở Một, Python Cung Cấp Hàm

with open['docs/readme.txt', 'w'] as f: f.write['Create a new text file!']

Code language: Python [python]
1. NÓ trả về VớI File

- Cú Phá:

try: with open['docs/readme.txt', 'w'] as f: f.write['Create a new text file!'] except FileNotFoundError: print["The 'docs' directory does not exist"]

Code language: Python [python]
4

In which:

  • try: with open['docs/readme.txt', 'w'] as f: f.write['Create a new text file!'] except FileNotFoundError: print["The 'docs' directory does not exist"]

    Code language: Python [python]
    5: Đối số file_name là một giá trị chuỗi chứa tun của các file mà bạn muốn truy cập.
  • try: with open['docs/readme.txt', 'w'] as f: f.write['Create a new text file!'] except FileNotFoundError: print["The 'docs' directory does not exist"]

    Code language: Python [python]
    6: Các access_mode xác định các chế độ của ôn
  • try: with open['docs/readme.txt', 'w'] as f: f.write['Create a new text file!'] except FileNotFoundError: print["The 'docs' directory does not exist"]

    Code language: Python [python]
    7: NếU Buffer ĐượC Thiết Lập Là 0, Nghĩa là sẽ Không đó Nếu xác Đị hnh lÀ 1, thì trình ôn ôn Nếu là số nguynênn lớn hơn 1, thì hoạt Đêm Nếu là số âm, thì kích cỡ bộ đun sẽ là mặc Định.

Dưới đây là một danh sách các

Cách thứcMô tả
rMở Tệp Chỉ Để
R+Mở File Để ĐọC Và GHI
RBMở Tệp Trong Chế Độ ĐọC Cho Đ-Nh Dạng NH Con tr
RB+Mở Tệp Để ĐọC Và GHI Trong ĐịNH Dạng NHị Phân. Con tr
wTạO Một File Mới Để Ghe, Nếu File
W+TạO Một File Mới Để
WBMở File Trong Chế Độ GHI Trong Đêm Hi Dạng NHị Phân. NếU File Đang tồn tại, thÌ
WB+Mở Tệp Để ĐọC Và GHI Trong ĐịNH Dạng NHị Phân. Nếu File tồn tại thì GHI Đè Nội Dung Của nó, Nếu File Không tồn tại thì tạo Một File
mộtMở File Để GHI thênm vào cuối, nếu không tìm thấy file sẽ tạo mới một
A+Mở File Để ĐọC Và GHI THEM VÀO CUốI FILE,
ABMở Tệp Trong Chế Độ Phụ lục Trong Chế Độ NHị Phân. Con trỏ là ở Cuối tập tin nếu tập tin nào đ tồn tại. NếU File Không tồn tại, thì tạo một file mới để ghe
AB+Mở File Trong Để ĐọC Và nối Trong Đêm Hi Dạng NHị Phân. Con trỏ Tệp tại cuối nếu Tệp đun tồn tại. Nếu Không tồn tại thì tạo một file

- Tệp Tính CủA

Thuộc TínhMô tả
rMở Tệp Chỉ Để
R+Mở File Để ĐọC Và GHI
RBMở Tệp Trong Chế Độ ĐọC Cho Đ-Nh Dạng NH Con tr

RB+

#!/usr/bin/python

# Mở file
file = open["plc.txt", "wb"]
print "Tên của file là: ", file.name
print "File có đóng hoặc không? : ", file.closed
print "Chế độ mở file : ", file.mode

Mở Tệp Để ĐọC Và GHI Trong ĐịNH Dạng NHị Phân. Con tr

Tên của file là:  plc.txt
File có đóng hoặc không? :  False
Chế độ mở file :  wb

w

TạO Một File Mới Để Ghe, Nếu File

** - Cú pháp: **

try: with open['docs/readme.txt', 'w'] as f: f.write['Create a new text file!'] except FileNotFoundError: print["The 'docs' directory does not exist"]

Code language: Python [python]
9

** - Ví dụ:**

#!/usr/bin/python

# Mở file
file = open["plc.txt", "r"]

# Đóng file
file.close[]

3. Đọc File

Giả sử chúng ta có một file

The 'docs' directory does not exist

Code language: plaintext [plaintext]
0 với nội dung như sau:

with open['readme.txt', 'w'] as f: f.write['Create a new text file!']

Code language: JavaScript [javascript]
0

3.1. Phương thức read

- Cú pháp:

The 'docs' directory does not exist

Code language: plaintext [plaintext]
1

Phương thức này trả về một chuỗi có kích thước bằng

The 'docs' directory does not exist

Code language: plaintext [plaintext]
2. Nếu không truyền

The 'docs' directory does not exist

Code language: plaintext [plaintext]
2 thì toàn bộ nội dung của file sẽ được đọc.

**- Ví dụ: **

with open['readme.txt', 'w'] as f: f.write['Create a new text file!']

Code language: JavaScript [javascript]
1

Kết quả in ra màn hình:

with open['readme.txt', 'w'] as f: f.write['Create a new text file!']

Code language: JavaScript [javascript]
2

3.2. Phương thức readline

- Cú pháp:

The 'docs' directory does not exist

Code language: plaintext [plaintext]
4

Phương thức này trả về một chuỗi có kích thước bằng

The 'docs' directory does not exist

Code language: plaintext [plaintext]
2. Nếu không truyền

The 'docs' directory does not exist

Code language: plaintext [plaintext]
2 thì toàn bộ nội dung của file sẽ được đọc.

**- Ví dụ: **

with open['readme.txt', 'w'] as f: f.write['Create a new text file!']

Code language: JavaScript [javascript]
3

Kết quả in ra màn hình:

with open['readme.txt', 'w'] as f: f.write['Create a new text file!']

Code language: JavaScript [javascript]
4

3.2. Phương thức readline

Phương thức này cho phép đọc một dòng trong file và trả về chuỗi.

** - Ví dụ: **

The 'docs' directory does not exist

Code language: plaintext [plaintext]
6

4. Ghi File

**- Ví dụ: **

with open['readme.txt', 'w'] as f: f.write['Create a new text file!']

Code language: JavaScript [javascript]
5

Kết quả in ra màn hình:

3.2. Phương thức readline

Phương thức này cho phép đọc một dòng trong file và trả về chuỗi.

** - Ví dụ: **

- Cú pháp:

with open['readme.txt', 'x'] as f: f.write['Create a new text file!']

Code language: Python [python]
0

Phương thức này trả về một chuỗi có kích thước bằng

The 'docs' directory does not exist

Code language: plaintext [plaintext]
2. Nếu không truyền

The 'docs' directory does not exist

Code language: plaintext [plaintext]
2 thì toàn bộ nội dung của file sẽ được đọc.

with open['readme.txt', 'w'] as f: f.write['Create a new text file!']

Code language: JavaScript [javascript]
7

**- Ví dụ: **

Kết quả in ra màn hình:

- Cú pháp:

with open['readme.txt', 'x'] as f: f.write['Create a new text file!']

Code language: Python [python]
2

**- Ví dụ: **

Kết quả in ra màn hình:

3.2. Phương thức readline

Phương thức này cho phép đọc một dòng trong file và trả về chuỗi.

** - Ví dụ: **

  • 4. Ghi File
  • Tương tự đọc file, để ghi một file ta cần mở file bằng cú pháp để ghi và sử dụng phương thức

    The 'docs' directory does not exist

    Code language: plaintext [plaintext]
    5 để ghi vào.
    • **- Cú pháp: **

    • Phương thức này cho phép ghi một chuỗi có nội dung là string vào vị trí của con trỏ trong file.

Và sau đây là nội dung bên trong file

The 'docs' directory does not exist

Code language: plaintext [plaintext]
7 sau khi thực hiện ghi file thành công.

with open['readme.txt', 'w'] as f: f.write['Create a new text file!']

Code language: JavaScript [javascript]
9

with open['readme.txt', 'w'] as f: f.write['Create a new text file!']

Code language: JavaScript [javascript]
6

with open['docs/readme.txt', 'w'] as f: f.write['Create a new text file!']

Code language: Python [python]
0

5. Thay tên File

Phương thức

The 'docs' directory does not exist

Code language: plaintext [plaintext]
8 trong module

The 'docs' directory does not exist

Code language: plaintext [plaintext]
9 được sử dụng để thay tên file. Phương thức này nhận hai tham số là tên file cũ và tên file mới.

  • //docs.python.org/3.5/tutorial/inputoutput.html#reading-and-writing-files
  • - Ví dụ:

Bài Viết Liên Quan

Chủ Đề