Hướng dẫn how to create and write to text file in python - cách tạo và ghi vào tệp văn bản trong 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['readme.txt', 'w'] as f: f.write['Create a new text file!']

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

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

Code language: JavaScript [javascript]
6 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['readme.txt', 'w'] as f: f.write['Create a new text file!']

Code language: JavaScript [javascript]
6. Hàm

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

Code language: JavaScript [javascript]
6 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['readme.txt', 'w'] as f: f.write['Create a new text file!']

Code language: JavaScript [javascript]
9 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]
    0 - Mở một tập tin để viết. Nếu tệp không tồn tại, hàm

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

    Code language: JavaScript [javascript]
    6 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]
    2 - Mở một tệp để tạo độc quyền. Nếu tệp tồn tại, hàm

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

    Code language: JavaScript [javascript]
    6 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]
    4]. 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

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

Code language: Python [python]
5 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

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

Code language: Python [python]
5 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ụ:

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

Code language: Python [python]
7, bạn cần đảm bảo rằng thư mục

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

Code language: Python [python]
8 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

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

Code language: Python [python]
8 không tồn tại. Do đó, nó không thể tạo tệp

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

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

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

Code language: Python [python]
8 và sau đó tạo tệp

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

Code language: Python [python]
5 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]
2 khi gọi hàm

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

Code language: JavaScript [javascript]
6:

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['readme.txt', 'w'] as f: f.write['Create a new text file!']

    Code language: JavaScript [javascript]
    6 với chế độ

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

    Code language: Python [python]
    0 hoặc

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

    Code language: Python [python]
    2 để 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?

Tóm tắt: Trong hướng dẫn này, bạn sẽ tìm hiểu nhiều cách khác nhau để viết các tệp văn bản bằng Python.: in this tutorial, you’ll learn various ways to write text files in Python.

TL;DR

Sau đây minh họa cách ghi chuỗi vào tệp văn bản:

with open['readme.txt', 'w'] as f: f.write['readme']

Code language: JavaScript [javascript]

Các bước để ghi vào tệp văn bản

Để ghi vào tệp văn bản bằng Python, bạn làm theo các bước sau:

  • Đầu tiên, hãy mở tệp văn bản để viết [hoặc nối] bằng hàm

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

    Code language: JavaScript [javascript]
    6.
  • Thứ hai, ghi vào tệp văn bản bằng phương thức

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

    Code language: JavaScript [javascript]
    9 hoặc

    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]
    0.
  • Thứ ba, đóng tệp bằng phương thức

    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]
    1.

Sau đây cho thấy cú pháp cơ bản của hàm

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

Code language: JavaScript [javascript]
6:

f = open[file, mode]

Hàm

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

Code language: JavaScript [javascript]
6 chấp nhận nhiều tham số. Nhưng bạn sẽ tập trung vào hai người đầu tiên:

  • Tham số

    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 chỉ định đường dẫn đến tệp văn bản mà bạn muốn mở để viết.
  • Tham số

    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 Chỉ định chế độ mà bạn muốn mở tệp văn bản.

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

Cách thứcSự mô tả

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

Code language: Python [python]
0
Mở một tệp văn bản để viết. Nếu tệp tồn tại, chức năng sẽ cắt giảm tất cả các nội dung ngay khi bạn mở nó. Nếu tệp không tồn tại, chức năng sẽ tạo một tệp mới.

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
Mở một tệp văn bản để nối thêm văn bản. Nếu tệp tồn tại, chức năng nối các nội dung ở cuối tệp.
++Mở một tệp văn bản để cập nhật [cả đọc và viết].

Hàm

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

Code language: JavaScript [javascript]
6 trả về một đối tượng tệp có hai phương thức hữu ích để ghi văn bản vào tệp:

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

Code language: JavaScript [javascript]
9 và

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]
0.

  • Phương thức

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

    Code language: JavaScript [javascript]
    9 ghi một chuỗi vào một tệp văn bản.
  • Phương thức

    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]
    0 Viết danh sách các chuỗi vào một tệp cùng một lúc.

Phương 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]
0 chấp nhận một đối tượng có thể lặp lại, không chỉ là một danh sách, vì vậy bạn có thể chuyển một bộ chuỗi, một tập hợp các chuỗi, v.v., cho phương thức

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]
0.

Để viết một dòng vào một tệp văn bản, bạn cần thêm một ký tự dòng mới:

f.write['\n'] f.writelines['\n']

Code language: JavaScript [javascript]

Viết ví dụ về tệp văn bản

Ví dụ sau đây cho thấy cách sử dụng chức năng

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

Code language: JavaScript [javascript]
9 để ghi danh sách các văn bản vào tệp văn bản:

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

Code language: JavaScript [javascript]
0

Nếu tệp readme.txt không tồn tại, hàm

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

Code language: JavaScript [javascript]
6 sẽ tạo một tệp mới.

Những điều sau đây cho thấy cách viết danh sách các chuỗi văn bản vào tệp văn bản:

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

Code language: JavaScript [javascript]
1

Nếu bạn coi từng yếu tố của danh sách là một dòng, bạn cần kết hợp nó với ký tự mới như thế này:

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

Code language: JavaScript [javascript]
2Appending Tệp văn bản

Appending text files

Để nối vào tệp văn bản, bạn cần mở tệp văn bản cho chế độ nối tiếp. Ví dụ sau đây nối các dòng mới vào tệp

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

Code language: Python [python]
5:

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

Code language: JavaScript [javascript]
3

Output:

Ghi vào tệp văn bản UTF-8

Nếu bạn viết các ký tự UTF-8 vào tệp văn bản bằng mã từ các ví dụ trước, bạn sẽ gặp lỗi như thế này:

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

Code language: JavaScript [javascript]
4

Để mở một tệp và ghi các ký tự UTF-8 vào một tệp, bạn cần chuyển tham số

The 'docs' directory does not exist

Code language: plaintext [plaintext]
8 cho hàm

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

Code language: JavaScript [javascript]
6.

Ví dụ sau đây cho thấy cách ghi các ký tự UTF-8 vào tệp văn bản:

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

Code language: JavaScript [javascript]
5

Bản tóm tắt

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

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

    Code language: JavaScript [javascript]
    6 với chế độ

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

    Code language: Python [python]
    1 hoặc

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

    Code language: Python [python]
    2 để mở tệp văn bản để nối thêm.
  • Luôn đóng tệp sau khi hoàn thành việc viết bằng phương thức

    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]
    1 hoặc sử dụng câu lệnh

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

    Code language: Python [python]
    4 khi mở tệp.
  • Sử dụng các phương thức

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

    Code language: JavaScript [javascript]
    9 và

    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]
    0 để ghi vào tệp văn bản.
  • Chuyển

    The 'docs' directory does not exist

    Code language: plaintext [plaintext]
    8 cho hàm

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

    Code language: JavaScript [javascript]
    6 để ghi các ký tự UTF-8 vào một tệp.

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

Làm thế nào để bạn viết một tệp văn bản trong tập lệnh Python?

Các bước để ghi vào các tệp văn bản để ghi vào tệp văn bản bằng Python, bạn làm theo các bước sau: Đầu tiên, hãy mở tệp văn bản để ghi [hoặc nối] bằng hàm Open []. Thứ hai, ghi vào tệp văn bản bằng phương thức write [] hoặc writeLines []. Thứ ba, đóng tệp bằng phương thức đóng [].open the text file for writing [or append] using the open[] function. Second, write to the text file using the write[] or writelines[] method. Third, close the file using the close[] method.

Làm thế nào để bạn tạo một tệp TXT?

Có một số cách:..
Trình chỉnh sửa trong IDE của bạn sẽ làm tốt.....
Notepad là một trình soạn thảo sẽ tạo các tệp văn bản.....
Có những biên tập viên khác cũng sẽ hoạt động.....
Microsoft Word có thể tạo một tệp văn bản, nhưng bạn phải lưu chính xác.....
WordPad sẽ lưu một tệp văn bản, nhưng một lần nữa, loại mặc định là rtf [văn bản phong phú] ..

Có thể tạo một tệp văn bản trong Python không?

Tạo một tệp nếu không tồn tại với python từ đường dẫn nhập pathlib dir_path = path ['c: \ temp'] file_name = 'myDocument.txt 'file_path = dir_path.tham gia [file_name] # Kiểm tra nếu thư mục có tồn tại nếu dir_path.is_dir []: # Kiểm tra xem tệp đã tồn tại nếu FILE_PATH.

Làm cách nào để tạo một tệp văn bản trong một thư mục trong Python?

Tạo một tệp văn bản trống, chúng ta có thể tạo một tệp bằng hàm tích hợp Open [].Chuyển tên tệp và chế độ truy cập cho hàm Open [] để tạo tệp.Chế độ truy cập Chỉ định mục đích mở một tệp.Dưới đây là danh sách các chế độ truy cập để tạo tệp A.using the built-in function open[] . Pass the file name and access mode to the open[] function to create a file. Access mode specifies the purpose of opening a file. Below is the list of access modes for creating an a file.

Bài Viết Liên Quan

Chủ Đề