Hướng dẫn write data to file python - ghi dữ liệu vào tệp 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.


Làm thế nào để bạn viết dữ liệu vào một tệp trong Python?

Tệp Python Viết..
❮ Trước Sau ❯.
Thí dụ. Mở tệp "demofile2.txt" và nối nội dung vào tệp: f = open ("demofile2.txt", "a") f.write ("Bây giờ tệp có nhiều nội dung hơn!") F.close () .. ..
Thí dụ. Mở tệp "demofile3.txt" và ghi đè nội dung: f = open ("demofile3.txt", "w") f.write ("woops! ....
❮ Trước Sau ❯.

Thí dụ. Mở tệp "demofile2.txt" và nối nội dung vào tệp: f = open ("demofile2.txt", "a") f.write ("Bây giờ tệp có nhiều nội dung hơn!") F.close () .. ..

Sử dụng định dạng chuỗi để viết một biến để sử dụng tệp mở (tệp, chế độ) với tên đường dẫn của tệp dưới dạng tệp và chế độ là "W" để mở tệp để ghi. Tệp gọi. Viết (dữ liệu) với dữ liệu là định dạng chuỗi " %s %d" theo sau là %và một tuple chứa một chuỗi của tên biến và biến. Sử dụng tệp. Use open(file, mode) with the pathname of a file as file and mode as "w" to open the file for writing. Call file. write(data) with data as the string formats "%s %d" followed by % and a tuple containing a string of the variable name, and the variable. Use file.

Làm cách nào để tạo và ghi vào một tệp trong Python?

Để tạo và ghi vào một tệp mới, hãy sử dụng mở với tùy chọn W W W.Tùy chọn W W W sẽ xóa bất kỳ tệp hiện có nào trước đây và tạo một tệp mới để viết.Nếu bạn muốn nối vào một tệp hiện có, thì hãy sử dụng câu lệnh Open với tùy chọn của A A.Trong chế độ Phụ lục, Python sẽ tạo tệp nếu nó không tồn tại.use open with “w” option. The “w” option will delete any previous existing file and create a new file to write. If you want to append to an existing file, then use open statement with “a” option. In append mode, Python will create the file if it does not exist.

Làm thế nào để bạn viết dữ liệu vào các tệp?

Để ghi dữ liệu vào một tệp bằng lớp FileOutputStream được hiển thị trong ví dụ sau.Nó cũng yêu cầu tạo đối tượng của lớp với tên tệp để ghi dữ liệu vào một tệp.Ở đây, nội dung chuỗi được chuyển đổi thành mảng byte được ghi vào tệp bằng cách sử dụng phương thức write ().using FileOutputStream class is shown in the following example. It also requires creating the object of the class with the filename to write data into a file. Here, the string content is converted into the byte array that is written into the file by using the write() method.