Tôi có hai tệp tôi muốn mở:
file = open['textures.txt', 'w']
file = open['to_decode.txt', 'w']
Sau đó, tôi muốn viết thư cho cả hai một cách riêng biệt:
file.write["Username: " + username + " Textures: " + textures]
file.write[textures]
Điều đầu tiên là cho lần mở đầu tiên và thứ hai là lần thứ hai. Làm thế nào tôi sẽ làm điều này?
Hỏi ngày 12 tháng 9 năm 2015 lúc 14:29Sep 12, 2015 at 14:29
3
Bạn đang ghi đè biến biến file
với lần mở thứ hai, vì vậy tất cả các chữ viết sẽ được hướng đến đó. Thay vào đó, bạn nên sử dụng hai biến:
textures_file = open['textures.txt', 'w']
decode_file = open['to_decode.txt', 'w']
textures_file.write["Username: " + username + " Textures: " + textures]
decode_file.write[textures]
Đã trả lời ngày 12 tháng 9 năm 2015 lúc 14:34Sep 12, 2015 at 14:34
MureinikmureinikMureinik
283K51 Huy hiệu vàng290 Huy hiệu bạc327 Huy hiệu đồng51 gold badges290 silver badges327 bronze badges
Đặt tên cho tệp của bạn con trỏ hai điều khác nhau, tức là không phải cả "tệp".
file1 = open...
file2 = open...
file1.write...
file2.write...
Ngay bây giờ, khai báo "Tệp" thứ hai mà bạn đang thực hiện là viết quá nhiều, tệp chỉ có tệp chỉ chỉ vào "to_decode.txt".
Đã trả lời ngày 12 tháng 9 năm 2015 lúc 14:33Sep 12, 2015 at 14:33
Troyshutroyshutroyshu
4325 huy hiệu bạc11 huy hiệu đồng5 silver badges11 bronze badges
Bạn có thể sử dụng "với" để tránh đề cập rõ ràng về file.close []. Sau đó, bạn không cần phải đóng nó - Python sẽ tự động thực hiện nó trong quá trình thu gom rác hoặc khi thoát khỏi chương trình.
with open['textures.txt', 'w'] as file1,open['to_decode.txt', 'w'] as file2:
file1.write["Username: " + username + " Textures: " + textures]
file2.write[textures]
Đã trả lời ngày 12 tháng 9 năm 2015 lúc 14:39Sep 12, 2015 at 14:39
Ahsanul Haqueahsanul HaqueAhsanul Haque
102K4 Huy hiệu vàng37 Huy hiệu bạc51 Huy hiệu Đồng4 gold badges37 silver badges51 bronze badges
1
Chỉ cần đặt cho họ những cái tên khác nhau:
f1 = open['textures.txt', 'w']
f2 = open['to_decode.txt', 'w']
f1.write["Username: " + username + " Textures: " + textures]
f2.write[textures]
Như những người khác đã đề cập, tệp là tên của một hàm tích hợp, vì vậy sử dụng tên đó cho biến cục bộ của bạn là một lựa chọn tồi.
Đã trả lời ngày 12 tháng 9 năm 2015 lúc 14:34Sep 12, 2015 at 14:34
stonesam92stonesam92stonesam92
Mureinikmureinik2 gold badges18 silver badges24 bronze badges
283K51 Huy hiệu vàng290 Huy hiệu bạc327 Huy hiệu đồng
file1 = open['textures.txt', 'w']
file2 = open['to_decode.txt', 'w']
Đặt tên cho tệp của bạn con trỏ hai điều khác nhau, tức là không phải cả "tệp".
file1.write["Username: " + username + " Textures: " + textures]
file2.write[textures]
Đã trả lời ngày 12 tháng 9 năm 2015 lúc 14:34Sep 12, 2015 at 14:34
MureinikmureinikShawn Mehan
283K51 Huy hiệu vàng290 Huy hiệu bạc327 Huy hiệu đồng9 gold badges30 silver badges50 bronze badges
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
ĐọcReading and Writing text files in Python
Bàn luận
Điều kiện tiên quyết: Đọc và viết tệp văn bản bằng Python
Python cung cấp khả năng mở cũng như làm việc với nhiều tệp cùng một lúc. Các tệp khác nhau có thể được mở ở các chế độ khác nhau, để mô phỏng việc viết đồng thời hoặc đọc từ các tệp này. Một số lượng tệp tùy ý có thể được mở với phương thức Open [] được hỗ trợ trong phiên bản Python 2.7 hoặc LỚN. & NBSP; with open[file_1] as f1, open[file_2] as f2
Parameters:
- Cú pháp được sử dụng để mở nhiều tệp
- Cú pháp: với mở [file_1] là f1, mở [file_2] dưới dạng f2
File_1: Chỉ định đường dẫn của tệp đầu tiên
File_2: Chỉ định đường dẫn của tệp thứ hai
file1.txt
file2.txt
Tên khác nhau được cung cấp cho các tập tin khác nhau. Các tệp có thể được mở trong các chế độ đọc, ghi hoặc nối tương ứng. Hoạt động được thực hiện đồng bộ và cả hai tệp được mở cùng một lúc. Theo mặc định, các tệp được mở để hỗ trợ các hoạt động đọc. & NBSP;
Tệp văn bản để trình diễn
- Các bước cần thiết
- Các bước được sử dụng để mở nhiều tệp cùng nhau trong Python:
- Cả hai tệp đều được mở bằng phương thức Open [] bằng cách sử dụng các tên khác nhau cho mỗi tệp
Nội dung của các tệp có thể được truy cập bằng phương thức readline [].
Các hoạt động đọc/ghi khác nhau có thể được thực hiện trên nội dung của các tệp này.
Python3
file.write["Username: " + username + " Textures: " + textures]
file.write[textures]
0file.write["Username: " + username + " Textures: " + textures]
file.write[textures]
1file.write["Username: " + username + " Textures: " + textures]
file.write[textures]
2file.write["Username: " + username + " Textures: " + textures]
file.write[textures]
3file.write["Username: " + username + " Textures: " + textures]
file.write[textures]
4file.write["Username: " + username + " Textures: " + textures]
file.write[textures]
1file.write["Username: " + username + " Textures: " + textures]
file.write[textures]
2file.write["Username: " + username + " Textures: " + textures]
file.write[textures]
7file.write["Username: " + username + " Textures: " + textures]
file.write[textures]
8file.write["Username: " + username + " Textures: " + textures]
file.write[textures]
9textures_file = open['textures.txt', 'w']
decode_file = open['to_decode.txt', 'w']
textures_file.write["Username: " + username + " Textures: " + textures]
decode_file.write[textures]
0textures_file = open['textures.txt', 'w']
decode_file = open['to_decode.txt', 'w']
textures_file.write["Username: " + username + " Textures: " + textures]
decode_file.write[textures]
1 textures_file = open['textures.txt', 'w']
decode_file = open['to_decode.txt', 'w']
textures_file.write["Username: " + username + " Textures: " + textures]
decode_file.write[textures]
2file.write["Username: " + username + " Textures: " + textures]
file.write[textures]
9textures_file = open['textures.txt', 'w']
decode_file = open['to_decode.txt', 'w']
textures_file.write["Username: " + username + " Textures: " + textures]
decode_file.write[textures]
4textures_file = open['textures.txt', 'w']
decode_file = open['to_decode.txt', 'w']
textures_file.write["Username: " + username + " Textures: " + textures]
decode_file.write[textures]
1 textures_file = open['textures.txt', 'w']
decode_file = open['to_decode.txt', 'w']
textures_file.write["Username: " + username + " Textures: " + textures]
decode_file.write[textures]
6file.write["Username: " + username + " Textures: " + textures]
file.write[textures]
9textures_file = open['textures.txt', 'w']
decode_file = open['to_decode.txt', 'w']
textures_file.write["Username: " + username + " Textures: " + textures]
decode_file.write[textures]
8textures_file = open['textures.txt', 'w']
decode_file = open['to_decode.txt', 'w']
textures_file.write["Username: " + username + " Textures: " + textures]
decode_file.write[textures]
9Output:
Geeksforgeeks is a complete portal. Try coding here!
Ví dụ 2:
& nbsp; mở tệp1 ở chế độ đọc và file2 ở chế độ viết. Mã sau đây cho biết lưu trữ nội dung của một tệp trong một tệp khác. & NBSP;
Python3
file.write["Username: " + username + " Textures: " + textures]
file.write[textures]
0file.write["Username: " + username + " Textures: " + textures]
file.write[textures]
1file.write["Username: " + username + " Textures: " + textures]
file.write[textures]
2file1 = open...
file2 = open...
file1.write...
file2.write...
3file1 = open...
file2 = open...
file1.write...
file2.write...
4file1 = open...
file2 = open...
file1.write...
file2.write...
5file.write["Username: " + username + " Textures: " + textures]
file.write[textures]
4file.write["Username: " + username + " Textures: " + textures]
file.write[textures]
1file.write["Username: " + username + " Textures: " + textures]
file.write[textures]
2file1 = open...
file2 = open...
file1.write...
file2.write...
9file1 = open...
file2 = open...
file1.write...
file2.write...
4with open['textures.txt', 'w'] as file1,open['to_decode.txt', 'w'] as file2:
file1.write["Username: " + username + " Textures: " + textures]
file2.write[textures]
1file.write["Username: " + username + " Textures: " + textures]
file.write[textures]
8with open['textures.txt', 'w'] as file1,open['to_decode.txt', 'w'] as file2:
file1.write["Username: " + username + " Textures: " + textures]
file2.write[textures]
3with open['textures.txt', 'w'] as file1,open['to_decode.txt', 'w'] as file2:
file1.write["Username: " + username + " Textures: " + textures]
file2.write[textures]
4Output:
Nội dung của File2 sau khi hoạt động này như sau: