Cách chuyển đổi tệp Python thành tệp văn bản

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

  • tệp văn bản. Trong loại tệp này, mỗi dòng văn bản được kết thúc bằng một ký tự đặc biệt gọi là EOL (End of Line), là ký tự dòng mới ('\n') theo mặc định trong python
  • tập tin nhị phân. Trong loại tệp này, không có dấu kết thúc cho một dòng và dữ liệu được lưu trữ sau khi chuyển đổi thành ngôn ngữ nhị phân mà máy có thể hiểu được

Trong bài viết này, chúng tôi sẽ tập trung vào việc mở, đóng, đọc và ghi dữ liệu trong một tệp văn bản

Chế độ truy cập tệp

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

  1. Chỉ đọc ('r'). Mở tệp văn bản để đọc. Tay cầm được định vị ở đầu tệp. Nếu tệp không tồn tại, sẽ phát sinh lỗi I/O. Đây cũng là chế độ mặc định trong đó tệp được mở
  2. Đọc và Viết ('r+'). Mở tệp để đọc và ghi. Tay cầm được định vị ở đầu tệp. Tăng lỗi I/O nếu tệp không tồn tại
  3. Chỉ ghi ('w'). Mở tệp để ghi. Đối với các tệp hiện có, dữ liệu bị cắt bớt và ghi đè. Tay cầm được định vị ở đầu tệp. Tạo tệp nếu tệp không tồn tại
  4. Viết và Đọc ('w+'). Mở tệp để đọc và ghi. Đối với một tệp hiện có, dữ liệu bị cắt bớt và ghi đè. Tay cầm được định vị ở đầu tệp
  5. Chỉ nối thêm ('a'). Mở tệp để ghi. 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 ghi sẽ được chèn vào cuối, sau dữ liệu hiện có
  6. Nối và Đọc ('a+'). Mở tệp để đọc và ghi. 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 ghi sẽ được chèn vào cuối, sau dữ liệu hiện có

Cách tệp được tải vào bộ nhớ chính

Có hai loại bộ nhớ trong máy tính. e. Bộ nhớ chính và bộ nhớ phụ mọi tệp bạn đã lưu hoặc bất kỳ ai đã lưu đều nằm trên bộ nhớ phụ khiến mọi dữ liệu trong bộ nhớ chính bị xóa khi tắt máy tính. Vì vậy, khi bạn cần thay đổi bất kỳ tệp văn bản nào hoặc chỉ để làm việc với chúng trong python, bạn cần tải tệp đó vào bộ nhớ chính. Python tương tác với các tệp được tải trong bộ nhớ chính hoặc bộ nhớ chính thông qua “trình xử lý tệp” ( Đây là cách hệ điều hành của bạn cấp quyền truy cập cho python để tương tác với tệp bạn đã mở bằng cách tìm kiếm tệp trong bộ nhớ của nó nếu tìm thấy, nó sẽ trả về một trình xử lý tệp và sau đó

Mở một tệp

Nó được thực hiện bằng hàm open(). Không cần nhập mô-đun cho chức năng này

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

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 phải được ghi thay cho tên tệp. Ghi chú. Chữ r được đặt trước tên tệp để ngăn các ký tự trong chuỗi tên tệp được coi là ký tự đặc biệt. Ví dụ: nếu có \temp trong địa chỉ tệp, thì \t được coi là ký tự tab và sẽ xảy ra lỗi do địa chỉ không hợp lệ. Chữ r làm cho chuỗi thô, nghĩa là nó báo rằng chuỗi không có bất kỳ ký tự đặc biệt nào. Có thể bỏ qua r nếu tệp nằm trong cùng thư mục và địa chỉ không được đặt.  

con trăn




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

# (same directory) in append mode and

File_object.write(str1)
0
File_object.write(str1)
1
File_object.write(str1)
2
File_object.write(str1)
3
File_object.write(str1)
4
File_object.write(str1)
5
File_object.write(str1)
6
File_object.write(str1)
7

File_object.write(str1)
8

File_object.write(str1)
9

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

File_object.writelines(L) for L = [str1, str2, str3] 
1
File_object.write(str1)
1
File_object.write(str1)
2
File_object.writelines(L) for L = [str1, str2, str3] 
4_______11_______5
File_object.write(str1)
5
File_object.writelines(L) for L = [str1, str2, str3] 
7
File_object.write(str1)
7

Ở đây, file1 được tạo làm đối tượng cho MyFile1 và file2 làm đối tượng cho MyFile2

Đóng một tập tin

close() đóng tệp và giải phóng không gian bộ nhớ mà tệp đó có được. Nó được sử dụng khi tệp không còn cần thiết hoặc nếu nó được mở ở một chế độ tệp khác. tệp_đối tượng. Thoát()

con trăn




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

File_object.read([n])
0

File_object.write(str1)
0
File_object.write(str1)
1
File_object.write(str1)
2
File_object.write(str1)
3
File_object.read([n])
5
File_object.write(str1)
5
File_object.write(str1)
6
File_object.write(str1)
7

File_object.read([n])
9

Viết vào một tập tin

Có hai cách để ghi vào một tập tin

  1. viết(). Chèn chuỗi str1 vào một dòng trong tệp văn bản
File_object.write(str1)
  1. 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] 

Đọc từ một tập tin

Có ba cách để đọc dữ liệu từ một tệp văn bản

  1. đọc(). Trả về các byte đã đọc dưới dạng một chuỗi. Đọc n byte, nếu không chỉ định n thì đọc toàn bộ tệp
File_object.read([n])
  1. dòng đọc (). Đọc một dòng của tệp và trả về dưới dạng một chuỗi. Đối với n được chỉ định, đọc tối đa n byte. Tuy nhiên, không đọc nhiều hơn một dòng, ngay cả khi n vượt quá độ dài của dòng
File_object.readline([n])
  1. đường đọc (). Đọc tất cả các dòng và trả về chúng dưới dạng mỗi dòng một phần tử chuỗi trong danh sách
  File_object.readlines()

Ghi chú. '\ n' được coi là một ký tự đặc biệt gồm hai byte

Python3




File_object.readline([n])
0

File_object.readline([n])
1

File_object.write(str1)
0
File_object.write(str1)
1
File_object.write(str1)
2_______1_______3
File_object.readline([n])
6
File_object.write(str1)
5
File_object.readline([n])
8
File_object.write(str1)
7

  File_object.readlines()
0
File_object.write(str1)
1
  File_object.readlines()
2
  File_object.readlines()
3_______1_______5
  File_object.readlines()
5
File_object.write(str1)
5
  File_object.readlines()
7
  File_object.readlines()
8

File_object.write(str1)
8

Output of Read function is 
Hello 
This is Delhi 
This is Paris 
This is London 


Output of Readline function is 
Hello 


Output of Read(9) function is 
Hello 
Th

Output of Readline(9) function is 
Hello 

Output of Readlines function is 
['Hello \n', 'This is Delhi \n', 'This is Paris \n', 'This is London \n']
0

Output of Read function is 
Hello 
This is Delhi 
This is Paris 
This is London 


Output of Readline function is 
Hello 


Output of Read(9) function is 
Hello 
Th

Output of Readline(9) function is 
Hello 

Output of Readlines function is 
['Hello \n', 'This is Delhi \n', 'This is Paris \n', 'This is London \n']
1_______56_______2
File_object.write(str1)
7

Output of Read function is 
Hello 
This is Delhi 
This is Paris 
This is London 


Output of Readline function is 
Hello 


Output of Read(9) function is 
Hello 
Th

Output of Readline(9) function is 
Hello 

Output of Readlines function is 
['Hello \n', 'This is Delhi \n', 'This is Paris \n', 'This is London \n']
4

File_object.read([n])
9_______56_______6

File_object.write(str1)
8

File_object.write(str1)
0
File_object.write(str1)
1
File_object.write(str1)
2
File_object.write(str1)
3
File_object.readline([n])
6
File_object.write(str1)
5
Output of Readlines after appending
['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n']

Output of Readlines after writing
['Tomorrow \n']
4
Output of Readlines after appending
['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n']

Output of Readlines after writing
['Tomorrow \n']
5

File_object.write(str1)
8

Output of Readlines after appending
['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n']

Output of Readlines after writing
['Tomorrow \n']
7_______1_______3
Output of Readlines after appending
['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n']

Output of Readlines after writing
['Tomorrow \n']
9
File_object.write(str1)
7

Output of Readlines after appending
['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n']

Output of Readlines after writing
['Tomorrow \n']
7_______209_______2

Output of Readlines after appending
['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n']

Output of Readlines after writing
['Tomorrow \n']
7_______209_______4

File_object.write(str1)
8

# Open function to open the file "MyFile1.txt" 6

# Open function to open the file "MyFile1.txt" 7

# Open function to open the file "MyFile1.txt" 8# Open function to open the file "MyFile1.txt" 9

Output of Readlines after appending
['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n']

Output of Readlines after writing
['Tomorrow \n']
5

File_object.write(str1)
8

Output of Readlines after appending
['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n']

Output of Readlines after writing
['Tomorrow \n']
7_______1_______3_______210_______4
File_object.write(str1)
7

Output of Readlines after appending
['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n']

Output of Readlines after writing
['Tomorrow \n']
7_______210_______7

Output of Readlines after appending
['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n']

Output of Readlines after writing
['Tomorrow \n']
7_______209_______4

File_object.write(str1)
8

# Open function to open the file "MyFile1.txt" 8# Open function to open the file "MyFile1.txt" 9

File_object.write(str1)
7

File_object.write(str1)
8

File_object.write(str1)
05

Output of Readlines after appending
['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n']

Output of Readlines after writing
['Tomorrow \n']
7_______1_______3
File_object.write(str1)
08
Output of Readlines after appending
['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n']

Output of Readlines after writing
['Tomorrow \n']
5

Output of Readlines after appending
['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n']

Output of Readlines after writing
['Tomorrow \n']
7_______1_______11
File_object.write(str1)
12
File_object.write(str1)
13

Output of Readlines after appending
['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n']

Output of Readlines after writing
['Tomorrow \n']
7_______209_______4

File_object.write(str1)
8

# Open function to open the file "MyFile1.txt" 8# Open function to open the file "MyFile1.txt" 9

File_object.write(str1)
7

File_object.write(str1)
8

Output of Readlines after appending
['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n']

Output of Readlines after writing
['Tomorrow \n']
7_______1_______3
File_object.write(str1)
23
Output of Readlines after appending
['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n']

Output of Readlines after writing
['Tomorrow \n']
5

Output of Readlines after appending
['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n']

Output of Readlines after writing
['Tomorrow \n']
7_______1_______26
File_object.write(str1)
12
File_object.write(str1)
13

File_object.write(str1)
8

# Open function to open the file "MyFile1.txt" 8# Open function to open the file "MyFile1.txt" 9

File_object.write(str1)
7

File_object.write(str1)
33

Output of Readlines after appending
['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n']

Output of Readlines after writing
['Tomorrow \n']
7_______1_______3
File_object.write(str1)
36
Output of Readlines after appending
['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n']

Output of Readlines after writing
['Tomorrow \n']
5

Output of Readlines after appending
['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n']

Output of Readlines after writing
['Tomorrow \n']
7_______1_______39

Output of Readlines after appending
['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n']

Output of Readlines after writing
['Tomorrow \n']
7_______209_______4

File_object.read([n])
9

đầu ra

Output of Read function is 
Hello 
This is Delhi 
This is Paris 
This is London 


Output of Readline function is 
Hello 


Output of Read(9) function is 
Hello 
Th

Output of Readline(9) function is 
Hello 

Output of Readlines function is 
['Hello \n', 'This is Delhi \n', 'This is Paris \n', 'This is London \n']

Thêm vào một tập tin

Python3




File_object.write(str1)
43

File_object.write(str1)
44

File_object.write(str1)
0
File_object.write(str1)
1
File_object.write(str1)
2_______1_______3
File_object.readline([n])
6
File_object.write(str1)
5
File_object.readline([n])
8
File_object.write(str1)
7

  File_object.readlines()
0
File_object.write(str1)
1
  File_object.readlines()
2
  File_object.readlines()
3_______1_______5
  File_object.readlines()
5
File_object.write(str1)
5
  File_object.readlines()
7
  File_object.readlines()
8

Output of Read function is 
Hello 
This is Delhi 
This is Paris 
This is London 


Output of Readline function is 
Hello 


Output of Read(9) function is 
Hello 
Th

Output of Readline(9) function is 
Hello 

Output of Readlines function is 
['Hello \n', 'This is Delhi \n', 'This is Paris \n', 'This is London \n']
4

File_object.read([n])
9

File_object.write(str1)
8

File_object.write(str1)
65

File_object.write(str1)
0
File_object.write(str1)
1
File_object.write(str1)
2
File_object.write(str1)
3
File_object.readline([n])
6
File_object.write(str1)
5
File_object.write(str1)
6
File_object.write(str1)
7
File_object.write(str1)
74

Output of Read function is 
Hello 
This is Delhi 
This is Paris 
This is London 


Output of Readline function is 
Hello 


Output of Read(9) function is 
Hello 
Th

Output of Readline(9) function is 
Hello 

Output of Readlines function is 
['Hello \n', 'This is Delhi \n', 'This is Paris \n', 'This is London \n']
1_______1_______76
File_object.write(str1)
7

File_object.read([n])
9

File_object.write(str1)
8

File_object.write(str1)
0
File_object.write(str1)
1
File_object.write(str1)
2
File_object.write(str1)
3
File_object.readline([n])
6
File_object.write(str1)
5
File_object.write(str1)
86
File_object.write(str1)
7

Output of Readlines after appending
['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n']

Output of Readlines after writing
['Tomorrow \n']
7_______1_______3
File_object.write(str1)
90
Output of Readlines after appending
['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n']

Output of Readlines after writing
['Tomorrow \n']
5

Output of Readlines after appending
['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n']

Output of Readlines after writing
['Tomorrow \n']
7_______1_______93

Output of Readlines after appending
['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n']

Output of Readlines after writing
['Tomorrow \n']
7_______209_______4

File_object.read([n])
9

File_object.write(str1)
8

File_object.write(str1)
98

File_object.write(str1)
0
File_object.write(str1)
1
File_object.write(str1)
2_______1_______3
File_object.readline([n])
6
File_object.write(str1)
5
File_object.readline([n])
8
File_object.write(str1)
7
File_object.writelines(L) for L = [str1, str2, str3] 
07

Output of Read function is 
Hello 
This is Delhi 
This is Paris 
This is London 


Output of Readline function is 
Hello 


Output of Read(9) function is 
Hello 
Th

Output of Readline(9) function is 
Hello 

Output of Readlines function is 
['Hello \n', 'This is Delhi \n', 'This is Paris \n', 'This is London \n']
1
File_object.writelines(L) for L = [str1, str2, str3] 
09
File_object.write(str1)
7

File_object.read([n])
9

File_object.write(str1)
8

File_object.write(str1)
0
File_object.write(str1)
1
File_object.write(str1)
2
File_object.write(str1)
3
File_object.readline([n])
6
File_object.write(str1)
5
File_object.write(str1)
86
File_object.write(str1)
7

Output of Readlines after appending
['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n']

Output of Readlines after writing
['Tomorrow \n']
7_______1_______3
File_object.writelines(L) for L = [str1, str2, str3] 
23
Output of Readlines after appending
['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n']

Output of Readlines after writing
['Tomorrow \n']
5

Output of Readlines after appending
['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n']

Output of Readlines after writing
['Tomorrow \n']
7_______1_______93

Output of Readlines after appending
['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n']

Output of Readlines after writing
['Tomorrow \n']
7_______209_______4

File_object.read([n])
9

đầu ra

Output of Readlines after appending
['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n']

Output of Readlines after writing
['Tomorrow \n']

Bài viết liên quan. Đối tượng tệp trong Python

Bài viết này được đóng góp bởi Harshit Agrawal. Nếu bạn thích GeeksforGeeks và muốn đóng góp, bạn cũng có thể viết một bài báo bằng cách sử dụng write. chuyên viên máy tính. org hoặc gửi bài viết của bạn tới review-team@geeksforgeeks. tổ chức. Xem bài viết của bạn xuất hiện trên trang chính của GeeksforGeeks và trợ giúp các Geeks khác

Vui lòng viết bình luận nếu bạn thấy bất cứ điều gì không chính xác hoặc bạn muốn chia sẻ thêm thông tin về chủ đề thảo luận ở trên

Là một. py là một tệp văn bản?

Tệp PY là tệp chương trình hoặc tập lệnh được viết bằng Python, một ngôn ngữ lập trình hướng đối tượng được giải thích . Nó có thể được tạo và chỉnh sửa bằng trình soạn thảo văn bản, nhưng yêu cầu trình thông dịch Python để chạy. Các tệp PY thường được sử dụng để lập trình máy chủ web và các hệ thống máy tính quản trị khác.

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

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

Làm cách nào để chuyển đổi txt sang tệp Python?

Mở tệp TXT nguồn bằng Python. Gọi phương thức 'save()', chuyển tên tệp đầu ra có phần mở rộng DOC. Nhận kết quả chuyển đổi TXT dưới dạng DOC .