Tôi đã tò mò kiểm tra thêm về hiệu suất và tôi đã sử dụng câu trả lời của Martijn Pieters và Stephen Miller.
Tôi đã thử các chế độ nhị phân và văn bản với shutil
và không có shutil
. Tôi đã cố gắng hợp nhất 270 tệp.
Chế độ văn bản -
def using_shutil_text[outfilename]:
with open[outfilename, 'w'] as outfile:
for filename in glob.glob['*.txt']:
if filename == outfilename:
# don't want to copy the output into the output
continue
with open[filename, 'r'] as readfile:
shutil.copyfileobj[readfile, outfile]
def without_shutil_text[outfilename]:
with open[outfilename, 'w'] as outfile:
for filename in glob.glob['*.txt']:
if filename == outfilename:
# don't want to copy the output into the output
continue
with open[filename, 'r'] as readfile:
outfile.write[readfile.read[]]
Chế độ nhị phân -
def using_shutil_text[outfilename]:
with open[outfilename, 'wb'] as outfile:
for filename in glob.glob['*.txt']:
if filename == outfilename:
# don't want to copy the output into the output
continue
with open[filename, 'rb'] as readfile:
shutil.copyfileobj[readfile, outfile]
def without_shutil_text[outfilename]:
with open[outfilename, 'wb'] as outfile:
for filename in glob.glob['*.txt']:
if filename == outfilename:
# don't want to copy the output into the output
continue
with open[filename, 'rb'] as readfile:
outfile.write[readfile.read[]]
Thời gian chạy cho chế độ nhị phân -
Shutil - 20.161773920059204
Normal - 17.327500820159912
Thời gian chạy cho chế độ văn bản -
Shutil - 20.47757601737976
Normal - 13.718038082122803
Có vẻ như ở cả hai chế độ, SHOTIL thực hiện giống nhau trong khi chế độ văn bản nhanh hơn nhị phân.
HĐH: Mac OS 10.14 Mojave. MacBook Air 2017.
Giả sử rằng bạn có nhiều tệp .txt
và bạn muốn kết hợp tất cả chúng vào một tệp ____8 duy nhất. Giả sử rằng các tệp .txt
của bạn nằm trong thư mục
def using_shutil_text[outfilename]:
with open[outfilename, 'wb'] as outfile:
for filename in glob.glob['*.txt']:
if filename == outfilename:
# don't want to copy the output into the output
continue
with open[filename, 'rb'] as readfile:
shutil.copyfileobj[readfile, outfile]
def without_shutil_text[outfilename]:
with open[outfilename, 'wb'] as outfile:
for filename in glob.glob['*.txt']:
if filename == outfilename:
# don't want to copy the output into the output
continue
with open[filename, 'rb'] as readfile:
outfile.write[readfile.read[]]
1. Sau đó, bạn sẽ cần phải có được con đường của họ:import os # find all the txt files in the dataset folder inputs = [] for file in os.listdir["dataset"]: if file.endswith[".txt"]: inputs.append[os.path.join["dataset", file]] # concatanate all txt files in a file called merged_file.txt with open['merged_file.txt', 'w'] as outfile: for fname in inputs: with open[fname, encoding="utf-8", errors='ignore'] as infile: outfile.write[infile.read[]]
Với đoạn trích ở trên, chúng tôi đã quản lý để kết hợp tất cả chúng vào một tệp được gọi là
def using_shutil_text[outfilename]:
with open[outfilename, 'wb'] as outfile:
for filename in glob.glob['*.txt']:
if filename == outfilename:
# don't want to copy the output into the output
continue
with open[filename, 'rb'] as readfile:
shutil.copyfileobj[readfile, outfile]
def without_shutil_text[outfilename]:
with open[outfilename, 'wb'] as outfile:
for filename in glob.glob['*.txt']:
if filename == outfilename:
# don't want to copy the output into the output
continue
with open[filename, 'rb'] as readfile:
outfile.write[readfile.read[]]
2. Trong trường hợp các tệp lớn, bạn có thể làm việc như sau:large, you can work as
follows:with open['merged_file.txt', 'w'] as outfile: for fname in inputs: with open[fname, encoding="utf-8", errors='ignore'] as infile: for line in infile: outfile.write[line]
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
Prerequisite:
- Đọc
- Bàn luận
Xử lý tập tin
Approach:
- hệ điều hành
- Python là một ngôn ngữ mạnh mẽ, cực kỳ có khả năng ngay cả khi xử lý tập tin. Trong bài viết này, chúng tôi sẽ tìm hiểu cách đọc nhiều tệp văn bản từ một thư mục bằng Python.
- Nhập các mô -đun
- Thêm đường dẫn của thư mục
- Thay đổi thư mục
- Nhận danh sách một tệp từ một thư mụcFile Handling
Lặp lại thông qua danh sách tệp và kiểm tra xem phần mở rộng của tệp có ở định dạng .txt hay không.
- Nếu tệp văn bản tồn tại, hãy đọc tệp bằng cách sử dụng xử lýFile method in Python used to change the current working directory to specified path. It takes only a single argument as new directory path.
Các chức năng được sử dụng: os.chdir[path]
Parameters:
- Phương thức OS.Chdir [] trong Python được sử dụng để thay đổi thư mục làm việc hiện tại thành đường dẫn được chỉ định. Nó chỉ lấy một đối số duy nhất làm đường dẫn thư mục mới. A complete path of directory to be changed to new directory path.
Cú pháp: OS.Chdir [Path] Doesn’t return any value
- Đường dẫn: Một đường dẫn hoàn chỉnh của thư mục sẽ được thay đổi thành đường dẫn thư mục mới. method in python is used to get the list of all files and directories in the specified directory. If we don’t specify any directory, then list of files and directories in the current working directory will be returned.
Trả về: không trả lại bất kỳ giá trị nào os.listdir[path]
Parameters:
- Phương thức Os.ListDir [] trong Python được sử dụng để lấy danh sách tất cả các tệp và thư mục trong thư mục được chỉ định. Nếu chúng tôi không chỉ định bất kỳ thư mục nào, thì danh sách các tệp và thư mục trong thư mục làm việc hiện tại sẽ được trả về.
Cú pháp: Os.ListDir [Path] This method returns the list of all files and directories in the specified path. The return type of this method is list.
đường dẫn [tùy chọn]: đường dẫn của thư mục
Program:
Python3
Loại trả về: Phương thức này trả về danh sách tất cả các tệp và thư mục trong đường dẫn được chỉ định. Loại trả về của phương pháp này là danh sách.
Dưới đây là việc thực hiện:
def using_shutil_text[outfilename]:
with open[outfilename, 'wb'] as outfile:
for filename in glob.glob['*.txt']:
if filename == outfilename:
# don't want to copy the output into the output
continue
with open[filename, 'rb'] as readfile:
shutil.copyfileobj[readfile, outfile]
def without_shutil_text[outfilename]:
with open[outfilename, 'wb'] as outfile:
for filename in glob.glob['*.txt']:
if filename == outfilename:
# don't want to copy the output into the output
continue
with open[filename, 'rb'] as readfile:
outfile.write[readfile.read[]]
8def using_shutil_text[outfilename]:
with open[outfilename, 'wb'] as outfile:
for filename in glob.glob['*.txt']:
if filename == outfilename:
# don't want to copy the output into the output
continue
with open[filename, 'rb'] as readfile:
shutil.copyfileobj[readfile, outfile]
def without_shutil_text[outfilename]:
with open[outfilename, 'wb'] as outfile:
for filename in glob.glob['*.txt']:
if filename == outfilename:
# don't want to copy the output into the output
continue
with open[filename, 'rb'] as readfile:
outfile.write[readfile.read[]]
3 def using_shutil_text[outfilename]:
with open[outfilename, 'wb'] as outfile:
for filename in glob.glob['*.txt']:
if filename == outfilename:
# don't want to copy the output into the output
continue
with open[filename, 'rb'] as readfile:
shutil.copyfileobj[readfile, outfile]
def without_shutil_text[outfilename]:
with open[outfilename, 'wb'] as outfile:
for filename in glob.glob['*.txt']:
if filename == outfilename:
# don't want to copy the output into the output
continue
with open[filename, 'rb'] as readfile:
outfile.write[readfile.read[]]
4def using_shutil_text[outfilename]:
with open[outfilename, 'wb'] as outfile:
for filename in glob.glob['*.txt']:
if filename == outfilename:
# don't want to copy the output into the output
continue
with open[filename, 'rb'] as readfile:
shutil.copyfileobj[readfile, outfile]
def without_shutil_text[outfilename]:
with open[outfilename, 'wb'] as outfile:
for filename in glob.glob['*.txt']:
if filename == outfilename:
# don't want to copy the output into the output
continue
with open[filename, 'rb'] as readfile:
outfile.write[readfile.read[]]
5def using_shutil_text[outfilename]:
with open[outfilename, 'wb'] as outfile:
for filename in glob.glob['*.txt']:
if filename == outfilename:
# don't want to copy the output into the output
continue
with open[filename, 'rb'] as readfile:
shutil.copyfileobj[readfile, outfile]
def without_shutil_text[outfilename]:
with open[outfilename, 'wb'] as outfile:
for filename in glob.glob['*.txt']:
if filename == outfilename:
# don't want to copy the output into the output
continue
with open[filename, 'rb'] as readfile:
outfile.write[readfile.read[]]
6 def using_shutil_text[outfilename]:
with open[outfilename, 'wb'] as outfile:
for filename in glob.glob['*.txt']:
if filename == outfilename:
# don't want to copy the output into the output
continue
with open[filename, 'rb'] as readfile:
shutil.copyfileobj[readfile, outfile]
def without_shutil_text[outfilename]:
with open[outfilename, 'wb'] as outfile:
for filename in glob.glob['*.txt']:
if filename == outfilename:
# don't want to copy the output into the output
continue
with open[filename, 'rb'] as readfile:
outfile.write[readfile.read[]]
7Shutil - 20.161773920059204
Normal - 17.327500820159912
7Shutil - 20.161773920059204
Normal - 17.327500820159912
8Shutil - 20.161773920059204
Normal - 17.327500820159912
9def using_shutil_text[outfilename]:
with open[outfilename, 'wb'] as outfile:
for filename in glob.glob['*.txt']:
if filename == outfilename:
# don't want to copy the output into the output
continue
with open[filename, 'rb'] as readfile:
shutil.copyfileobj[readfile, outfile]
def without_shutil_text[outfilename]:
with open[outfilename, 'wb'] as outfile:
for filename in glob.glob['*.txt']:
if filename == outfilename:
# don't want to copy the output into the output
continue
with open[filename, 'rb'] as readfile:
outfile.write[readfile.read[]]
9 Shutil - 20.161773920059204
Normal - 17.327500820159912
0Shutil - 20.161773920059204
Normal - 17.327500820159912
1Shutil - 20.161773920059204
Normal - 17.327500820159912
2223Shutil - 20.161773920059204
Normal - 17.327500820159912
4Shutil - 20.161773920059204
Normal - 17.327500820159912
5Shutil - 20.161773920059204
Normal - 17.327500820159912
6Shutil - 20.47757601737976
Normal - 13.718038082122803
0 Shutil - 20.47757601737976
Normal - 13.718038082122803
1 Shutil - 20.47757601737976
Normal - 13.718038082122803
2 Shutil - 20.47757601737976
Normal - 13.718038082122803
3Shutil - 20.161773920059204
Normal - 17.327500820159912
7import os # find all the txt files in the dataset folder inputs = [] for file in os.listdir["dataset"]: if file.endswith[".txt"]: inputs.append[os.path.join["dataset", file]] # concatanate all txt files in a file called merged_file.txt with open['merged_file.txt', 'w'] as outfile: for fname in inputs: with open[fname, encoding="utf-8", errors='ignore'] as infile: outfile.write[infile.read[]]6
Output:
//media.geeksforgeeks.org/wp-content/uploads/20210125102530/FreeOnlineScreenRecorderProject4.mp4