Hướng dẫn python combine all text files in a directory - python kết hợp tất cả các tệp văn bản trong một thư mục

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

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

    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[]]
    
    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[]]
    
    4

    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[]]
    
    5
    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[]]
    
    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[]]
    
    7

    Shutil - 20.161773920059204
    Normal - 17.327500820159912
    
    7
    Shutil - 20.161773920059204
    Normal - 17.327500820159912
    
    8
    Shutil - 20.161773920059204
    Normal - 17.327500820159912
    
    9

    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[]]
    
    9
    Shutil - 20.161773920059204
    Normal - 17.327500820159912
    
    0

    Shutil - 20.161773920059204
    Normal - 17.327500820159912
    
    1
    Shutil - 20.161773920059204
    Normal - 17.327500820159912
    
    2223
    Shutil - 20.161773920059204
    Normal - 17.327500820159912
    
    4
    Shutil - 20.161773920059204
    Normal - 17.327500820159912
    
    5
    Shutil - 20.161773920059204
    Normal - 17.327500820159912
    
    6

    Shutil - 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
    
    3

    Shutil - 20.161773920059204
    Normal - 17.327500820159912
    
    7
    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[]]
    
    6

    Output:

    //media.geeksforgeeks.org/wp-content/uploads/20210125102530/FreeOnlineScreenRecorderProject4.mp4

    Làm cách nào để hợp nhất tất cả các tệp TXT vào một thư mục trong Python?

    Sử dụng "Cat *. TXT> Tất cả.

    Làm thế nào để bạn kết hợp nhiều tệp văn bản 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 để kết hợp nhiều tệp văn bản thành một?

    Hai tùy chọn nhanh để kết hợp các tập tin văn bản.Mở hai tệp bạn muốn hợp nhất.Chọn tất cả văn bản [lệnh+a/ctrl+a] từ một tài liệu, sau đó dán nó vào tài liệu mới [lệnh+v/ctrl+v].Lặp lại các bước cho tài liệu thứ hai.Điều này sẽ kết thúc kết hợp văn bản của cả hai tài liệu thành một.Open the two files you want to merge. Select all text [Command+A/Ctrl+A] from one document, then paste it into the new document [Command+V/Ctrl+V]. Repeat steps for the second document. This will finish combining the text of both documents into one.

    Làm cách nào để đọc nhiều tệp văn bản trong một thư mục trong Python?

    Nhập mô -đun HĐH trong sổ ghi chép của bạn.Xác định một đường dẫn nơi các tệp văn bản được đặt trong hệ thống của bạn.Tạo một danh sách các tệp và lặp lại để tìm xem tất cả chúng có mở rộng chính xác hay không.Đọc các tệp bằng hàm được xác định trong mô -đun.

    Bài Viết Liên Quan

    Chủ Đề