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
Đọc
Example
Input : file1.txt file2.txt Output : Content of first file [before appending] - geeksfor Content of second file [before appending] - geeks Content of first file [after appending] - geeksforgeeks Content of second file [after appending] - geeks
Bàn luận
- Có hai tên tệp được nhập từ người dùng, nhiệm vụ là nối nội dung của tệp thứ hai vào nội dung của tệp đầu tiên.Example & nbsp; & nbsp;
- Thuật toán: & nbsp; & nbsp;
- Nhập tên của các tập tin.
- Mở cả hai tệp trong chế độ chỉ đọc bằng hàm Open [].
- In nội dung của các tệp trước khi thêm vào hàm đọc [].
- Đóng cả hai tệp bằng hàm đóng [].
- Mở tệp thứ nhất ở chế độ phụ lục và tệp thứ hai ở chế độ đọc.
- Nối nội dung của tệp thứ hai vào tệp thứ nhất bằng hàm write [].
- Định vị lại con trỏ của các tệp ở đầu bằng cách sử dụng hàm Seek [].
In nội dung của các tập tin được nối thêm.
file1.txt
file2.txt
python3
Đóng cả hai tập tin.
Giả sử các tệp văn bản File1.txt và File2.txt chứa dữ liệu sau.File1.TXT & nbsp; & nbsp;
firstfile
=
input
[
"Enter the name of first file "
__
import fileinput
with open[outfilename, 'w'] as fout, fileinput.input[filenames] as fin:
for line in fin:
fout.write[line]
1=
input
[
import fileinput
with open[outfilename, 'w'] as fout, fileinput.input[filenames] as fin:
for line in fin:
fout.write[line]
5import fileinput
with open[outfilename, 'w'] as fout, fileinput.input[filenames] as fin:
for line in fin:
fout.write[line]
0with open['outfilename', 'w'] as fout:
fin = fileinput.input[filenames]
for line in fin:
fout.write[line]
fin.close[]
9[
1nameOfFiles = ["moving files.py", "mysql_access.py", "stored procedure python-sql.py", "trial.py"] with open["advanced_file.py", "w"] as new_created_file: for name in nameOfFiles: with open[name] as file: for line in file: new_created_file.write[line] new_created_file.write["
"]
2nameOfFiles = ["moving files.py", "mysql_access.py", "stored procedure python-sql.py", "trial.py"] with open["advanced_file.py", "w"] as new_created_file: for name in nameOfFiles: with open[name] as file: for line in file: new_created_file.write[line] new_created_file.write["
"]
with open['outfilename', 'w'] as fout:
fin = fileinput.input[filenames]
for line in fin:
fout.write[line]
fin.close[]
9[
5nameOfFiles = ["moving files.py", "mysql_access.py", "stored procedure python-sql.py", "trial.py"] with open["advanced_file.py", "w"] as new_created_file: for name in nameOfFiles: with open[name] as file: for line in file: new_created_file.write[line] new_created_file.write["
"]
6nameOfFiles = ["moving files.py", "mysql_access.py", "stored procedure python-sql.py", "trial.py"] with open["advanced_file.py", "w"] as new_created_file: for name in nameOfFiles: with open[name] as file: for line in file: new_created_file.write[line] new_created_file.write["
"]
7nameOfFiles = ["moving files.py", "mysql_access.py", "stored procedure python-sql.py", "trial.py"] with open["advanced_file.py", "w"] as new_created_file: for name in nameOfFiles: with open[name] as file: for line in file: new_created_file.write[line] new_created_file.write["
"]
8nameOfFiles = ["moving files.py", "mysql_access.py", "stored procedure python-sql.py", "trial.py"] with open["advanced_file.py", "w"] as new_created_file: for name in nameOfFiles: with open[name] as file: for line in file: new_created_file.write[line] new_created_file.write["
"]
import fileinput
with open[outfilename, 'w'] as fout, fileinput.input[filenames] as fin:
for line in fin:
fout.write[line]
7=
import fileinput
with open[outfilename, 'w'] as fout, fileinput.input[filenames] as fin:
for line in fin:
fout.write[line]
9with open['outfilename', 'w'] as fout:
fin = fileinput.input[filenames]
for line in fin:
fout.write[line]
fin.close[]
0with open['outfilename', 'w'] as fout:
fin = fileinput.input[filenames]
for line in fin:
fout.write[line]
fin.close[]
1import fileinput
with open[outfilename, 'w'] as fout, fileinput.input[filenames] as fin:
for line in fin:
fout.write[line]
0import fileinput
with open[outfilename, 'w'] as fout, fileinput.input[filenames] as fin:
for line in fin:
fout.write[line]
1=
input
[
import fileinput
with open[outfilename, 'w'] as fout, fileinput.input[filenames] as fin:
for line in fin:
fout.write[line]
5import fileinput
with open[outfilename, 'w'] as fout, fileinput.input[filenames] as fin:
for line in fin:
fout.write[line]
0firstfile
1
firstfile
2firstfile
3
import fileinput
with open[outfilename, 'w'] as fout, fileinput.input[filenames] as fin:
for line in fin:
fout.write[line]
0firstfile
5firstfile
3
import fileinput
with open[outfilename, 'w'] as fout, fileinput.input[filenames] as fin:
for line in fin:
fout.write[line]
0with open['outfilename', 'w'] as fout:
fin = fileinput.input[filenames]
for line in fin:
fout.write[line]
fin.close[]
9[
=
02nameOfFiles = ["moving files.py", "mysql_access.py", "stored procedure python-sql.py", "trial.py"] with open["advanced_file.py", "w"] as new_created_file: for name in nameOfFiles: with open[name] as file: for line in file: new_created_file.write[line] new_created_file.write["
"]
with open['outfilename', 'w'] as fout:
fin = fileinput.input[filenames]
for line in fin:
fout.write[line]
fin.close[]
9[
=
46nameOfFiles = ["moving files.py", "mysql_access.py", "stored procedure python-sql.py", "trial.py"] with open["advanced_file.py", "w"] as new_created_file: for name in nameOfFiles: with open[name] as file: for line in file: new_created_file.write[line] new_created_file.write["
"]
7nameOfFiles = ["moving files.py", "mysql_access.py", "stored procedure python-sql.py", "trial.py"] with open["advanced_file.py", "w"] as new_created_file: for name in nameOfFiles: with open[name] as file: for line in file: new_created_file.write[line] new_created_file.write["
"]
8nameOfFiles = ["moving files.py", "mysql_access.py", "stored procedure python-sql.py", "trial.py"] with open["advanced_file.py", "w"] as new_created_file: for name in nameOfFiles: with open[name] as file: for line in file: new_created_file.write[line] new_created_file.write["
"]
7import fileinput
with open[outfilename, 'w'] as fout, fileinput.input[filenames] as fin:
for line in fin:
fout.write[line]
=
9import fileinput
with open[outfilename, 'w'] as fout, fileinput.input[filenames] as fin:
for line in fin:
fout.write[line]
0with open['outfilename', 'w'] as fout:
fin = fileinput.input[filenames]
for line in fin:
fout.write[line]
fin.close[]
1with open['outfilename', 'w'] as fout:
fin = fileinput.input[filenames]
for line in fin:
fout.write[line]
fin.close[]
0 import fileinput
with open[outfilename, 'w'] as fout, fileinput.input[filenames] as fin:
for line in fin:
fout.write[line]
Đó chính xác là những gì FileInput dành cho:
import fileinput
with open[outfilename, 'w'] as fout, fileinput.input[filenames] as fin:
for line in fin:
fout.write[line]
Đối với trường hợp sử dụng này, nó thực sự không đơn giản hơn nhiều so với việc lặp lại các tệp theo cách thủ công, nhưng trong các trường hợp khác, có một trình lặp duy nhất lặp lại trên tất cả các tệp như thể chúng là một tệp rất tiện dụng. .
Có một số tính năng tiện lợi khác trong =
8, như khả năng sửa đổi các tệp tại chỗ chỉ bằng cách lọc từng dòng.
Như đã lưu ý trong các ý kiến, và được thảo luận trong một bài đăng khác, =
8 cho Python 2.7 sẽ không hoạt động như đã chỉ ra. Ở đây sửa đổi một chút để làm cho mã Python 2.7 tuân thủ
with open['outfilename', 'w'] as fout:
fin = fileinput.input[filenames]
for line in fin:
fout.write[line]
fin.close[]
Python làm cho việc tạo các tệp mới đơn giản, đọc các tệp hiện có, nối dữ liệu hoặc thay thế dữ liệu trong các tệp hiện có. Với sự trợ giúp của một vài thư viện nguồn mở và bên thứ ba, nó có thể quản lý tất cả các loại tệp hiện đang được hỗ trợ.
Chúng tôi phải lặp lại tất cả các tệp cần thiết, thu thập dữ liệu của chúng và sau đó thêm nó vào một tệp mới để kết hợp một số tệp vào một tệp. Bài viết này trình bày cách sử dụng Python để kết hợp nhiều tệp vào một tệp.
Sử dụng các vòng lặp
Một danh sách các tên tệp hoặc đường dẫn tệp đến các tệp Python cần thiết có thể được tìm thấy trong mã Python bên dưới. Tiếp theo, Advanced_File.py được mở hoặc tạo.
Danh sách các tên tệp hoặc đường dẫn tệp sau đó được lặp lại. Mỗi tệp tạo ra một bộ mô tả tệp, đọc từng dòng nội dung của nó và sau đó ghi thông tin vào tệp Advanced_file.py.
Nó thêm một ký tự mới, hoặc, vào tệp mới ở cuối mỗi dòng.
, to the new file at the end of each line.
Ví dụ 1
Sau đây là một ví dụ để hợp nhất nhiều tệp nto một tệp sử dụng cho vòng lặp -
nameOfFiles = ["moving files.py", "mysql_access.py", "stored procedure python-sql.py", "trial.py"] with open["advanced_file.py", "w"] as new_created_file: for name in nameOfFiles: with open[name] as file: for line in file: new_created_file.write[line] new_created_file.write["
"]
Đầu ra
Như một đầu ra, một tệp Python mới được tạo với tên là Advanced_file, có tất cả các tệp Python được đề cập hiện có trong đó.advanced_file” which has all the existing mentioned python files in it.
Ví dụ - 2
Trong mã sau, chúng tôi đã mở tệp hiện có ở chế độ đọc và tệp mới được tạo, tức là Advanced_File ở chế độ ghi. Sau đó, chúng tôi đã đọc FRM rằng cả các tệp và thêm nó vào một chuỗi và viết dữ liệu từ chuỗi vào tệp được tạo mới. Cuối cùng, đã đóng các tệp -
info1 = info2 = "" with open['mysql_access.py'] as file: info1 = file.read[] with open['trial.py'] as file: info2 = file.read[] info1 += "
" info1 += info2 with open ['advanced_file.py', 'w'] as file: file.write[info1]
Đầu ra
Như một đầu ra, một tệp Python mới được tạo với tên là Advanced_file, có tất cả các tệp Python được đề cập hiện có trong đó.advanced_file” which has both the existing mentioned python files in it.
Ví dụ - 2
- Trong mã sau, chúng tôi đã mở tệp hiện có ở chế độ đọc và tệp mới được tạo, tức là Advanced_File ở chế độ ghi. Sau đó, chúng tôi đã đọc FRM rằng cả các tệp và thêm nó vào một chuỗi và viết dữ liệu từ chuỗi vào tệp được tạo mới. Cuối cùng, đã đóng các tệp -
- Là một đầu ra, một tệp python mới được tạo với tên là Advanced_file, có cả hai tệp python được đề cập hiện tại trong đó.
- Cập nhật vào ngày 18 tháng 8 năm 2022 08:00:00
- Câu hỏi và câu trả lời liên quan
- Làm thế nào để kết hợp hai tệp vào một tệp mới bằng Python?
- Làm thế nào để đổ một tệp nhị phân vào nhiều tệp bằng Python?
- Làm thế nào để hợp nhất nhiều tệp CSV thành một DataFrame Pandas?
- Hợp nhất nội dung của hai tệp thành tệp thứ ba bằng C
- Chương trình Java để hợp nhất hai tệp thành tệp thứ ba
- Làm thế nào để mở nhiều tệp bằng trình chọn tệp trong javafx?
- Chương trình Java để hợp nhất hai hoặc nhiều tệp thay thế vào tệp thứ ba
- Làm thế nào để hợp nhất tất cả các tệp CSV thành một DataFrame - Python Pandas?
- Python - Viết dữ liệu nhiều tệp vào tệp chính
- Làm thế nào để sao chép các tệp vào một thư mục mới bằng Python?
- Làm thế nào chúng ta có thể chia lớp Python thành nhiều tệp?
- Đổi tên nhiều tệp bằng Python