I'd like to ask you to recall Python txt content in a row

Asked 2 years ago, Updated 2 years ago, 119 views

The code you want to fill out is as follows.

There are several txt files in the folder, and you print out a list of them first I'd like to print out the contents of each txt file using repetitive statements. It's not combined.

Bringing the list I wrote a code to print out the content I don't know how to do both at once.

Please answer me.

python txt

2022-09-20 19:59

1 Answers

import os

def show_directory(path):
    r = []
    path += '/'
    files = os.listdir(path)
    for file in files:
        filename = os.path.join(path, file)
        ext = os.path.splitext(filename)[-1]
        if ext == '.txt':
            r.append(path+file)
    return r

def show_file(filelist):
    for file in filelist:
        with open(file) as f:
            print(file)
            for i in f:
                print(i, end='\n\n')

if __name__ == "__main__":
    tmp = show_directory('./ans')
    show_file(tmp)

There is no content of the source code, so I think the content of what to do will be limited, and you can refer to the aboveFor Use


2022-09-20 19:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.