Hướng dẫn how to write output file in python - cách ghi tệp đầu ra bằng python

Python cung cấp các chức năng sẵn có để tạo, viết và đọc các tệp. Có hai loại tệp có thể được xử lý trong Python, tệp văn bản thông thường và tệp nhị phân [được viết bằng ngôn ngữ nhị phân, 0s và 1s].

  • Tệp văn bản: Trong loại tệp này, mỗi dòng văn bản được chấm dứt với một ký tự đặc biệt có tên EOL [cuối dòng], là ký tự dòng mới [‘\ n,] trong Python theo mặc định. In this type of file, Each line of text is terminated with a special character called EOL [End of Line], which is the new line character [‘\n’] in python by default.
  • Tệp nhị phân: Trong loại tệp này, không có bộ hủy nào cho một dòng và dữ liệu được lưu trữ sau khi chuyển đổi nó thành ngôn ngữ nhị phân có thể hiểu bằng máy. In this type of file, there is no terminator for a line and the data is stored after converting it into machine-understandable binary language.

Lưu ý: Để biết thêm về xử lý tệp bấm vào đây. To know more about file handling click here.

Mục lục

  • Chế độ truy cập
  • Mở một tập tin
  • Đóng một tập tin
  • Viết vào tệp
    • Nối vào một tập tin
    • Với tuyên bố

Chế độ truy cập

Mở một tập tin

  1. Đóng một tập tin Open the file for writing. For an existing file, the data is truncated and over-written. The handle is positioned at the beginning of the file. Creates the file if the file does not exist.
  2. Viết vào tệp Open the file for reading and writing. For an existing file, data is truncated and over-written. The handle is positioned at the beginning of the file.
  3. Nối vào một tập tin Open the file for writing. The file is created if it does not exist. The handle is positioned at the end of the file. The data being written will be inserted at the end, after the existing data.

Với tuyên bố To know more about access mode click here.

Các chế độ truy cập chi phối loại hoạt động có thể trong tệp đã mở. Nó đề cập đến cách các tập tin sẽ được sử dụng sau khi nó mở. Các chế độ này cũng xác định vị trí của xử lý tệp trong tệp. Xử lý tệp giống như một con trỏ, xác định từ nơi dữ liệu phải được đọc hoặc ghi trong tệp. Các chế độ truy cập khác nhau để đọc tệp là -

Chỉ viết [‘W,]: Mở tệp để viết. Đối với một tệp hiện có, dữ liệu bị cắt cụt và viết quá mức. Tay cầm được định vị ở đầu tệp. Tạo tệp nếu tệp không tồn tại.

Syntax:

File_object = open[r"File_Name", "Access_Mode"]

Viết và đọc [‘W+,]: Mở tệp để đọc và viết. Đối với một tệp hiện có, dữ liệu bị cắt ngắn và viết quá mức. Tay cầm được định vị ở đầu tệp.

Chỉ nối thêm [‘A,]: Mở tệp để viết. Tệp được tạo nếu nó không tồn tại. Tay cầm được định vị ở cuối tệp. Dữ liệu được viết sẽ được chèn vào cuối, sau dữ liệu hiện có. The r is placed before filename to prevent the characters in filename string to be treated as special character. For example, if there is \temp in the file address, then \t is treated as the tab character and error is raised of invalid address. The r makes the string raw, that is, it tells that the string is without any special characters. The r can be ignored if the file is in same directory and address is not being placed.

Lưu ý: Để biết thêm về chế độ truy cập bấm vào đây.

Mở một tập tin

Nó được thực hiện bằng cách sử dụng hàm ____. Không có mô -đun nào được yêu cầu nhập khẩu cho chức năng này.

Đóng một tập tin

Viết vào tệp

Syntax:

File_object.close[]

Lưu ý: Để biết thêm về chế độ truy cập bấm vào đây.

File_object.writelines[L] for L = [str1, str2, str3] 
5

Viết vào tệp

Nối vào một tập tin

  1. Với tuyên bố Inserts the string str1 in a single line in the text file.
    File_object.write[str1]
    
  2. Các chế độ truy cập chi phối loại hoạt động có thể trong tệp đã mở. Nó đề cập đến cách các tập tin sẽ được sử dụng sau khi nó mở. Các chế độ này cũng xác định vị trí của xử lý tệp trong tệp. Xử lý tệp giống như một con trỏ, xác định từ nơi dữ liệu phải được đọc hoặc ghi trong tệp. Các chế độ truy cập khác nhau để đọc tệp là - For a list of string elements, each string is inserted in the text file. Used to insert multiple strings at a single time.
    File_object.writelines[L] for L = [str1, str2, str3] 
    

Chỉ viết [‘W,]: Mở tệp để viết. Đối với một tệp hiện có, dữ liệu bị cắt cụt và viết quá mức. Tay cầm được định vị ở đầu tệp. Tạo tệp nếu tệp không tồn tại.

File_object.writelines[L] for L = [str1, str2, str3] 
6 is treated as a special character of two bytes.

Example:

Viết và đọc [‘W+,]: Mở tệp để đọc và viết. Đối với một tệp hiện có, dữ liệu bị cắt ngắn và viết quá mức. Tay cầm được định vị ở đầu tệp.

Chỉ nối thêm [‘A,]: Mở tệp để viết. Tệp được tạo nếu nó không tồn tại. Tay cầm được định vị ở cuối tệp. Dữ liệu được viết sẽ được chèn vào cuối, sau dữ liệu hiện có.

Lưu ý: Để biết thêm về chế độ truy cập bấm vào đây.

Output of Readlines after appending
This is Delhi
This is Paris
This is London
Today


Output of Readlines after writing
Tomorrow
7

Output of Readlines after appending
This is Delhi
This is Paris
This is London
Today


Output of Readlines after writing
Tomorrow
8

Output of Readlines after appending
This is Delhi
This is Paris
This is London
Today


Output of Readlines after writing
Tomorrow
9

Mở một tập tin

with open filename as file:
8
with open filename as file:
9

Output of Readlines after appending
This is Delhi
This is Paris
This is London
Today


Output of Readlines after writing
Tomorrow
9

Output:

Hello
This is Delhi
This is Paris
This is London

Nối vào một tập tin

Với tuyên bố

Các chế độ truy cập chi phối loại hoạt động có thể trong tệp đã mở. Nó đề cập đến cách các tập tin sẽ được sử dụng sau khi nó mở. Các chế độ này cũng xác định vị trí của xử lý tệp trong tệp. Xử lý tệp giống như một con trỏ, xác định từ nơi dữ liệu phải được đọc hoặc ghi trong tệp. Các chế độ truy cập khác nhau để đọc tệp là -

Chỉ nối thêm [‘A,]: Mở tệp để viết. Tệp được tạo nếu nó không tồn tại. Tay cầm được định vị ở cuối tệp. Dữ liệu được viết sẽ được chèn vào cuối, sau dữ liệu hiện có.

Output of Readlines after appending
This is Delhi
This is Paris
This is London
Today


Output of Readlines after writing
Tomorrow
8

Output of Readlines after appending
This is Delhi
This is Paris
This is London
Today


Output of Readlines after writing
Tomorrow
9

Lưu ý: Để biết thêm về chế độ truy cập bấm vào đây.

Mở một tập tin

Output of Readlines after appending
This is Delhi
This is Paris
This is London
Today


Output of Readlines after writing
Tomorrow
9

Nó được thực hiện bằng cách sử dụng hàm ____. Không có mô -đun nào được yêu cầu nhập khẩu cho chức năng này.

with open filename as file:
8
File_object.close[]
3
File_object.close[]
12
Hello
This is Delhi
This is Paris
This is London
4

with open filename as file:
8
with open filename as file:
9

with open filename as file:
8
File_object.close[]
17

Output of Readlines after appending
This is Delhi
This is Paris
This is London
Today


Output of Readlines after writing
Tomorrow
9

Tệp phải tồn tại trong cùng thư mục với tệp chương trình Python khác, địa chỉ đầy đủ của tệp nên được viết tại chỗ của tên tệp.

r8

File_object.close[]
28
Hello
This is Delhi
This is Paris
This is London
4

Output of Readlines after appending
This is Delhi
This is Paris
This is London
Today


Output of Readlines after writing
Tomorrow
9

Các

with open filename as file:
8
File_object.close[]
3
File_object.close[]
41
Hello
This is Delhi
This is Paris
This is London
4

with open filename as file:
8
with open filename as file:
9

with open filename as file:
8
File_object.close[]
17

Output of Readlines after appending
This is Delhi
This is Paris
This is London
Today


Output of Readlines after writing
Tomorrow
9

Output:

Output of Readlines after appending
This is Delhi
This is Paris
This is London
Today


Output of Readlines after writing
Tomorrow

Với tuyên bố

File_object.close[]
48Statement Trong Python được sử dụng trong xử lý ngoại lệ để làm cho mã sạch hơn và dễ đọc hơn nhiều.Nó đơn giản hóa việc quản lý các tài nguyên chung như luồng tệp.Không giống như các triển khai trên, không cần phải gọi
File_object.close[]
49 khi sử dụng với câu lệnh.Bản thân tuyên bố
File_object.close[]
50 đảm bảo mua lại và phát hành các tài nguyên thích hợp.

Syntax:

with open filename as file:

Hello
This is Delhi
This is Paris
This is London
5
File_object.close[]
1
Hello
This is Delhi
This is Paris
This is London
7
Hello
This is Delhi
This is Paris
This is London
8
File_object.close[]
5
Output of Readlines after appending
This is Delhi
This is Paris
This is London
Today


Output of Readlines after writing
Tomorrow
0__15
Output of Readlines after appending
This is Delhi
This is Paris
This is London
Today


Output of Readlines after writing
Tomorrow
22

File_object.close[]
48
File_object.close[]
2
File_object.close[]
3
Hello
This is Delhi
This is Paris
This is London
5
File_object.close[]
5
File_object.close[]
6
File_object.close[]
66

File_object.close[]
67r8
File_object.close[]
69
Hello
This is Delhi
This is Paris
This is London
4

File_object.close[]
67
Output of Readlines after appending
This is Delhi
This is Paris
This is London
Today


Output of Readlines after writing
Tomorrow
8

File_object.close[]
48
File_object.close[]
2
File_object.close[]
3
Hello
This is Delhi
This is Paris
This is London
5
File_object.close[]
5
File_object.close[]
78
File_object.close[]
66

File_object.close[]
67
with open filename as file:
8
with open filename as file:
9

Output:

Hello
This is Delhi
This is Paris
This is London

LƯU Ý: Để biết thêm về tuyên bố bấm vào đây. To know more about with statement click here.


Bài Viết Liên Quan

Chủ Đề