Hướng dẫn python write to two files at once - python ghi vào hai tệp cùng một lúc

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ướng dẫn python write to two files at once - python ghi vào hai tệp cùng một lúc

Hỏi ngày 12 tháng 9 năm 2015 lúc 14:29Sep 12, 2015 at 14:29

Hướng dẫn python write to two files at once - python ghi vào hai tệp cùng một lúc

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

  • Đọc
  • Bàn luận
  • 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

    Hướng dẫn python write to two files at once - python ghi vào hai tệp cùng một lúc

    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)
    
    0
    file.write("Username: " + username + " Textures: " + textures)
    file.write(textures)
    
    1
    file.write("Username: " + username + " Textures: " + textures)
    file.write(textures)
    
    2
    file.write("Username: " + username + " Textures: " + textures)
    file.write(textures)
    
    3
    file.write("Username: " + username + " Textures: " + textures)
    file.write(textures)
    
    4
    file.write("Username: " + username + " Textures: " + textures)
    file.write(textures)
    
    1
    file.write("Username: " + username + " Textures: " + textures)
    file.write(textures)
    
    2
    file.write("Username: " + username + " Textures: " + textures)
    file.write(textures)
    
    7
    file.write("Username: " + username + " Textures: " + textures)
    file.write(textures)
    
    8

    file.write("Username: " + username + " Textures: " + textures)
    file.write(textures)
    
    9
    textures_file = open('textures.txt', 'w')
    decode_file = open('to_decode.txt', 'w')
    
    textures_file.write("Username: " + username + " Textures: " + textures)
    decode_file.write(textures)
    
    0
    textures_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)
    
    2

    file.write("Username: " + username + " Textures: " + textures)
    file.write(textures)
    
    9
    textures_file = open('textures.txt', 'w')
    decode_file = open('to_decode.txt', 'w')
    
    textures_file.write("Username: " + username + " Textures: " + textures)
    decode_file.write(textures)
    
    4
    textures_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)
    
    6

    file.write("Username: " + username + " Textures: " + textures)
    file.write(textures)
    
    9
    textures_file = open('textures.txt', 'w')
    decode_file = open('to_decode.txt', 'w')
    
    textures_file.write("Username: " + username + " Textures: " + textures)
    decode_file.write(textures)
    
    8
    textures_file = open('textures.txt', 'w')
    decode_file = open('to_decode.txt', 'w')
    
    textures_file.write("Username: " + username + " Textures: " + textures)
    decode_file.write(textures)
    
    9

    Output:

    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)
    
    0
    file.write("Username: " + username + " Textures: " + textures)
    file.write(textures)
    
    1
    file.write("Username: " + username + " Textures: " + textures)
    file.write(textures)
    
    2
    file1 = open...
    file2 = open...
    
    file1.write...
    file2.write...
    
    3
    file1 = open...
    file2 = open...
    
    file1.write...
    file2.write...
    
    4
    file1 = open...
    file2 = open...
    
    file1.write...
    file2.write...
    
    5
    file.write("Username: " + username + " Textures: " + textures)
    file.write(textures)
    
    4
    file.write("Username: " + username + " Textures: " + textures)
    file.write(textures)
    
    1
    file.write("Username: " + username + " Textures: " + textures)
    file.write(textures)
    
    2
    file1 = open...
    file2 = open...
    
    file1.write...
    file2.write...
    
    9
    file1 = open...
    file2 = open...
    
    file1.write...
    file2.write...
    
    4
    with open('textures.txt', 'w') as file1,open('to_decode.txt', 'w') as file2:
    
        file1.write("Username: " + username + " Textures: " + textures)
        file2.write(textures)
    
    1
    file.write("Username: " + username + " Textures: " + textures)
    file.write(textures)
    
    8

    with open('textures.txt', 'w') as file1,open('to_decode.txt', 'w') as file2:
    
        file1.write("Username: " + username + " Textures: " + textures)
        file2.write(textures)
    
    3
    with open('textures.txt', 'w') as file1,open('to_decode.txt', 'w') as file2:
    
        file1.write("Username: " + username + " Textures: " + textures)
        file2.write(textures)
    
    4

    Output:  

    Nội dung của File2 sau khi hoạt động này như sau:


    Bạn có thể viết vào hai tệp cùng một lúc trong Python không?

    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.. Different files can be opened in different modes, to simulate simultaneous writing or reading from these files.

    Làm thế nào để bạn viết nhiều tệp trong Python?

    Viết trong nhiều tập tin Python..
    fn = Open ("Path of Input File.txt", "R").
    fnew = fn.đọc().
    fs = fnew.chia ('\ n').
    Đối với giá trị trong fs:.
    f = [open ("đường dẫn của tệp đầu ra để ghi.txt" %i, 'w') cho i trong phạm vi (LEN (list_of_files))].
    f.Viết (giá trị).
    f.gần().

    Làm thế nào để bạn liên kết hai tệp trong Python?

    Sau đây là các bước để hợp nhất ...
    Mở tệp1.TXT và FILE2.txt trong chế độ đọc ..
    Mở File3.TXT trong chế độ ghi ..
    Đọc dữ liệu từ File1 và thêm nó vào một chuỗi ..
    Đọc dữ liệu từ File2 và nối dữ liệu của tệp này đến chuỗi trước ..
    Viết dữ liệu từ chuỗi đến File3 ..
    Đóng tất cả các tệp ..

    Làm cách nào để mở hai tệp cùng một lúc trong Python?

    Sử dụng Open () để mở nhiều tệp Sử dụng cú pháp với Open (file_1) là f1, mở (file_2) dưới dạng f2 với file_1 dưới dạng đường dẫn của tệp thứ nhất sẽ được mở và file_2 dưới dạng đường dẫn của tệp thứ hai được mở để mởMở cả hai tệp cùng một lúc. Use the syntax with open(file_1) as f1, open(file_2) as f2 with file_1 as the path of the first file to be opened and file_2 as the path of the second file to be opened to open both files at the same time.