文件名,目录,文本,python文件夹子文件夹

2024-09-29 23:33:11 发布

您现在位置:Python中文网/ 问答频道 /正文

在您的帮助下,我制作了一个脚本,从文件夹(/desktop/filme/1,2,3,4,5…)中提取子文件夹(/desktop/filme/1/fileone,/desktop/filme/1/filetwo,/desktop/filme/1/filetwo,/desktop/filme/1/filethree…)保存在另一个文件夹(/desktop/text/1.txt,/desktop/text/2.txt…)。与txt工作正常,我希望它保存在excel就像我保存它在txt,因为我使用xlwt。如果你有什么办法让它与其他天秤座一起工作,我会试试的。在

这是保存txt-s的脚本:

import os
import os.path

for dirname, dirnames, filenames in os.walk(r"C:/Users/s/Desktop/filme/"):

    for subdirname in dirnames:

        foldere_filme = os.path.join(dirname, subdirname)
        numarul_folderelor = foldere_filme.replace("C:/Users/s/Desktop/filme/", "")
        print numarul_folderelor #1,2,3,4,5...
        root_text = "C:/Users/s/Desktop/text/"+numarul_folderelor+".txt"
        fisier_text = open(""+root_text+"", "w")
        for filenames in os.listdir(foldere_filme):
            numele_pt_text = filenames.replace(".avi", "")
            print numele_pt_text
            fisier_text.writelines(
                                "Full Movie: \n\n\nwatch "
                                +numele_pt_text+" online free, \nwatch "
                                +numele_pt_text+" online")
        fisier_text.close()

我希望使用xlwt或其他任何工具来创建excel:

^{pr2}$

我会努力的,但我很乐意帮忙。在


Tags: textintxt文件夹ptforosusers
1条回答
网友
1楼 · 发布于 2024-09-29 23:33:11

但是为什么要打开和关闭每一行的文本文件呢?你可以这样做:

    numarul_folderelor = foldere_filme.replace("C:/Users/s/Desktop/filme/", "")
    print numarul_folderelor #1,2,3,4,5...

    root_text = "C:/Users/s/Desktop/text/"+numarul_folderelor+".txt"
    fisier_text = open(""+root_text+"", "w")

    for filenames in os.listdir(foldere_filme):
        numele_pt_text = filenames.replace(".avi", "")
        print numele_pt_text
        fisier_text.writelines(
                        "Full Movie: \n\n\nwatch "
                        +numele_pt_text+" online free, \nwatch "
                        +numele_pt_text+" online")
    fisier_text.close()

相关问题 更多 >

    热门问题