Cách lưu tệp trong mã python


Ghi vào một tập tin hiện có

Để ghi vào một tệp hiện có, bạn phải thêm một tham số vào hàm open()

"a" - Nối - sẽ nối vào cuối tệp

"w" - Viết - sẽ ghi đè lên bất kỳ nội dung hiện có

Thí dụ

Mở tệp "demofile2. txt" và nối thêm nội dung vào tệp

f = open("file demo2. txt", "a")
f. write("Bây giờ file có thêm nội dung. ")
f. Thoát()

#open và đọc tệp sau khi nối thêm
f = open("file demo2. txt", "r")
in (f. đọc())

Chạy ví dụ »

Thí dụ

Mở tệp "demofile3. txt" và ghi đè lên nội dung

f = open("file demo3. txt", "w")
f. viết ("Rất tiếc. Tôi đã xóa nội dung. ")
f. Thoát()

#mở và đọc tệp sau khi ghi đè
f = open("file demo3. txt", "r")
in (f. đọc())

Chạy ví dụ »

Ghi chú. phương thức "w" sẽ ghi đè lên toàn bộ tệp


Tạo một tệp mới

Để tạo một tệp mới trong Python, hãy sử dụng phương thức open(), với một trong các tham số sau

"x" - Tạo - sẽ tạo tệp, trả về lỗi nếu tệp tồn tại

"a" - Nối thêm - sẽ tạo tệp nếu tệp được chỉ định không tồn tại

"w" - Viết - sẽ tạo tệp nếu tệp được chỉ định không tồn tại

Thí dụ

Tạo một tệp có tên "myfile. txt"

f = open("tệp của tôi. txt", "x")

Kết quả. một tệp trống mới được tạo

Thí dụ

Tạo một tập tin mới nếu nó không tồn tại

f = open("tệp của tôi. txt", "w")



Python cho phép người dùng xử lý tệp (đọc, ghi, lưu và xóa tệp, v.v.). Vì Python, chúng tôi rất dễ dàng lưu nhiều định dạng tệp. Python có các chức năng tích hợp để lưu nhiều định dạng tệp

Mở tệp văn bản bằng Python

Việc mở một tệp đề cập đến việc tệp đã sẵn sàng để đọc hoặc ghi. Điều này có thể được thực hiện bằng hàm open()

cú pháp

File_object = open("File_Name", "Access_Mode")

Thông số

  • Tên_tệp. Tên tệp cần mở
  • Chế độ truy cập. Chế độ truy cập chi phối loại hoạt động có thể có trong tệp đã mở

Sau đây là các chế độ truy cập được sử dụng phổ biến nhất

  • Chỉ đọc ('r'). Mở tệp văn bản để đọc
  • Chỉ ghi ('w'). Mở tệp để ghi
  • Chỉ nối thêm ('a'). Mở tệp để ghi. Dữ liệu được ghi sẽ được chèn vào cuối, sau dữ liệu hiện có
  • Đọc và Viết ('r+'). Mở tệp để đọc và ghi

Ghi chú. Theo mặc định, Python giả định chế độ truy cập là read i. e (“r”)




# Python program to demonstrate 

# opening a file 

   

   

# Open function to open the file "myfile.txt"   

File_object.write(str1)
0

File_object.write(str1)
1

   

File_object.write(str1)
3_______1_______4
File_object.write(str1)
5
File_object.write(str1)
6
File_object.write(str1)
7
File_object.write(str1)
8

   

File_object.writelines(L) for L = [str1, str2, str3] 
0

File_object.writelines(L) for L = [str1, str2, str3] 
1
File_object.writelines(L) for L = [str1, str2, str3] 
2

   

File_object.writelines(L) for L = [str1, str2, str3] 
4

Ghi chú. Để biết thêm thông tin, hãy tham khảo Mở tệp bằng Python

Lưu tệp văn bản bằng Python

Sau khi tìm hiểu về cách mở File trong Python, hãy xem các cách để lưu nó. Mở một tệp mới ở chế độ ghi sẽ tạo một tệp và sau khi đóng tệp, các tệp sẽ tự động được lưu. Tuy nhiên, chúng ta cũng có thể ghi một số văn bản vào tệp. Python cung cấp hai phương thức cho cùng một

  • viết(). Chèn chuỗi str1 vào một dòng trong tệp văn bản.
    File_object.write(str1)
  • dòng viết (). Đối với danh sách các phần tử chuỗi, mỗi chuỗi được chèn vào tệp văn bản. Được sử dụng để chèn nhiều chuỗi cùng một lúc.
    File_object.writelines(L) for L = [str1, str2, str3] 

Thí dụ




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

File_object.writelines(L) for L = [str1, str2, str3] 
6

File_object.writelines(L) for L = [str1, str2, str3] 
7

File_object.writelines(L) for L = [str1, str2, str3] 
7

File_object.writelines(L) for L = [str1, str2, str3] 
9
File_object.write(str1)
4
File_object.write(str1)
5
File_object.write(str1)
6
with open filename as file:
     statement(s)
3
with open filename as file:
     statement(s)
4
with open filename as file:
     statement(s)
5
with open filename as file:
     statement(s)
6

File_object.writelines(L) for L = [str1, str2, str3] 
9
with open filename as file:
     statement(s)
8
with open filename as file:
     statement(s)
9
with open filename as file:
     statement(s)
6

File_object.writelines(L) for L = [str1, str2, str3] 
9open()2

đầu ra

Cách lưu tệp trong mã python

với tuyên bố

Câu lệnh open()3 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 phổ biến như luồng tệp. Không giống như các triển khai ở trên, không cần gọi open()4 khi sử dụng câu lệnh open()3. Bản thân câu lệnh open()3 đảm bảo việc thu thập và giải phóng tài nguyên một cách hợp lý

cú pháp

with open filename as file:
     statement(s)

Thí dụ




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

File_object.writelines(L) for L = [str1, str2, str3] 
6

File_object.writelines(L) for L = [str1, str2, str3] 
7

File_object.writelines(L) for L = [str1, str2, str3] 
7

open()3_______1_______5

File_object.write(str1)
6
with open filename as file:
     statement(s)
3
with open filename as file:
     statement(s)
4
with open filename as file:
     statement(s)
5# Python program to demonstrate 7
File_object.writelines(L) for L = [str1, str2, str3] 
9# Python program to demonstrate 9

# opening a file 0

# opening a file 1# opening a file 2

File_object.write(str1)
4 # opening a file 4# opening a file 5# opening a file 6

# opening a file 7# opening a file 8# opening a file 6

# opening a file 7   1# opening a file 6

# opening a file 7# opening a file 8

with open filename as file:
     statement(s)
4

# opening a file 7   7# opening a file 6

# opening a file 7# opening a file 8# opening a file 6

# opening a file 7   3   4

# opening a file 0

# opening a file 1

File_object.writelines(L) for L = [str1, str2, str3] 
9   8   9 # Open function to open the file "myfile.txt"   0 # Open function to open the file "myfile.txt"   1# Open function to open the file "myfile.txt"   2 # Open function to open the file "myfile.txt"   1# Open function to open the file "myfile.txt"   4 # Open function to open the file "myfile.txt"   5

đầu ra

Cách lưu tệp trong mã python

Ghi chú. Để biết thêm thông tin, hãy tham khảo Ghi vào tệp bằng Python

Lưu tệp CSV bằng Python

CSV là tệp Giá trị được phân tách bằng dấu phẩy được sử dụng rộng rãi nhất để đặt dữ liệu dạng bảng. Tệp CSV lưu trữ dữ liệu dạng bảng (số và văn bản) ở dạng văn bản thuần túy. Mỗi dòng của tệp là một bản ghi dữ liệu. Mỗi bản ghi bao gồm một hoặc nhiều trường, được phân tách bằng dấu phẩy. Python có mô-đun tích hợp có tên là # Open function to open the file "myfile.txt"   6 để ghi và lưu tệp CSV

Để lưu tệp CSV

  • Trước tiên, chúng ta cần nhập thư viện csv
  • Sau đó mở file như chúng ta thường làm nhưng thay vì viết nội dung lên đối tượng read_file, chúng ta tạo một đối tượng mới có tên là read_writer
  • Đối tượng này cung cấp cho chúng ta phương thức writelines() cho phép chúng ta đặt tất cả dữ liệu của hàng trong một lần nhập

Thí dụ




# Python program to demonstrate 

# Open function to open the file "myfile.txt"   8

   

   

File_object.write(str1)
01
File_object.write(str1)
02

# opening a file 0

File_object.write(str1)
04

File_object.write(str1)
05
File_object.write(str1)
4 # opening a file 4
File_object.write(str1)
08_______23_______4
File_object.write(str1)
10
with open filename as file:
     statement(s)
4
File_object.write(str1)
12
with open filename as file:
     statement(s)
4
File_object.write(str1)
14
File_object.write(str1)
15

# opening a file 0

File_object.write(str1)
17

File_object.write(str1)
18
File_object.write(str1)
4
File_object.write(str1)
20
File_object.write(str1)
21_______23_______4
File_object.write(str1)
23
with open filename as file:
     statement(s)
4
File_object.write(str1)
25
with open filename as file:
     statement(s)
4
File_object.write(str1)
27
File_object.write(str1)
28

File_object.write(str1)
29_______147_______4
File_object.write(str1)
31
with open filename as file:
     statement(s)
4
File_object.write(str1)
23
with open filename as file:
     statement(s)
4
File_object.write(str1)
25
with open filename as file:
     statement(s)
4
File_object.write(str1)
37
File_object.write(str1)
28

File_object.write(str1)
29_______147_______4
File_object.write(str1)
41
with open filename as file:
     statement(s)
4
File_object.write(str1)
43
with open filename as file:
     statement(s)
4
File_object.write(str1)
25
with open filename as file:
     statement(s)
4
File_object.write(str1)
47
File_object.write(str1)
28

File_object.write(str1)
29_______147_______4
File_object.write(str1)
51
with open filename as file:
     statement(s)
4
File_object.write(str1)
53
with open filename as file:
     statement(s)
4
File_object.write(str1)
55
with open filename as file:
     statement(s)
4
File_object.write(str1)
57
File_object.write(str1)
28

File_object.write(str1)
29_______147_______4
File_object.write(str1)
61
with open filename as file:
     statement(s)
4
File_object.write(str1)
63
with open filename as file:
     statement(s)
4
File_object.write(str1)
65
with open filename as file:
     statement(s)
4
File_object.write(str1)
67
File_object.write(str1)
28

File_object.write(str1)
29_______147_______4
File_object.write(str1)
71
with open filename as file:
     statement(s)
4
File_object.write(str1)
73
with open filename as file:
     statement(s)
4
File_object.write(str1)
25
with open filename as file:
     statement(s)
4
File_object.write(str1)
37
File_object.write(str1)
78

# opening a file 0

File_object.write(str1)
80

File_object.write(str1)
81
File_object.write(str1)
4
File_object.write(str1)
83

# opening a file 0

File_object.write(str1)
85

open()3_______1_______5

File_object.write(str1)
88
with open filename as file:
     statement(s)
5
File_object.write(str1)
90

# opening a file 1

File_object.write(str1)
92

# opening a file 1_______1_______94

File_object.write(str1)
4
File_object.write(str1)
96

File_object.write(str1)
29

# opening a file 1

File_object.write(str1)
99

# opening a file 1

File_object.writelines(L) for L = [str1, str2, str3] 
01

File_object.write(str1)
29

# opening a file 1____9_______04

# opening a file 1____9_______06

đầu ra

Cách lưu tệp trong mã python

Ghi chú. Để biết thêm thông tin, hãy tham khảo Viết tệp CSV bằng Python

Lưu tệp JSON bằng Python

Dạng đầy đủ của JSON là Ký hiệu đối tượng JavaScript. Điều đó có nghĩa là một tệp script (có thể thực thi) được tạo thành từ văn bản bằng ngôn ngữ lập trình, được sử dụng để lưu trữ và truyền dữ liệu. Python hỗ trợ JSON thông qua gói tích hợp có tên là

File_object.writelines(L) for L = [str1, str2, str3] 
07. Văn bản trong JSON được thực hiện thông qua chuỗi trích dẫn chứa giá trị trong ánh xạ khóa-giá trị trong phạm vi
File_object.writelines(L) for L = [str1, str2, str3] 
08

Mô-đun này cung cấp một phương thức có tên là

File_object.writelines(L) for L = [str1, str2, str3] 
09 để chuyển đổi các đối tượng Python thành các đối tượng json thích hợp