python:如何计算输出中出现的文件数?

b0zn9rqh  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(380)

我有一个python代码,可以翻译几个html页面。处理完每一页后,我指示打印消息向我显示文件名和消息“已翻译”。
现在,我收到以下打印消息(输出):

true-value-of-myself.html translated
I-love-books.html translated
my-laptop-is-still-here.html translated

但是如何计算输出中出现的文件数(文件名)?我需要更改输出,以便打印如下内容:

true-value-of-myself.html translated (1)
I-love-books.html translated (2)
my-laptop-is-still-here.html translated (3)

这是一段带有输出的代码:

directory = os.fsencode(files_from_folder)

def recursively_translate(node):

    for x in range(len(node.contents)):

        if isinstance(node.contents[x], str):

            if node.contents[x].strip() != '':

                try:

                    node.contents[x].replaceWith(ts.translate(node.contents[x]))

                except Exception as e:

                    print(e)

        elif node.contents[x] != None:

            recursively_translate(node.contents[x]) 
    .....

for file in os.listdir(directory): 
    filename = os.fsdecode(file) 
    print(filename) 

print(f'{filename} translated')
ki0zmccv

ki0zmccv1#

例如:

for i, file in enumerate(os.listdir(directory)):
    filename = os.fsdecode(file)
    # Do something

    print(f"{filename} translated ({i})")

相关问题