0

在您的帮助下,我制作了一个脚本,该脚本从文件夹 (/desktop/filme/) 中获取子文件夹 (/desktop/filme/1,2,3,4,5...) 的列表并保存每个文件名的名称另一个文件夹(/desktop/text/1.txt、/desktop/文本/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:

import os
import xlwt
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...
        wbk = xlwt.Workbook()
        sheet = wbk.add_sheet('sheet 1', cell_overwrite_ok=True)
        for filenames in os.listdir(foldere_filme):
            numele_pt_text = filenames.replace(".avi", "")
            print numele_pt_text
            sheet.write(0,0,numele_pt_text)
            sheet.write(1,0,numele_pt_text)
            sheet.write(2,0,numele_pt_text)
            sheet.write(3,0,numele_pt_text)
            sheet.write(4,0,numele_pt_text)
            sheet.write(5,0,numele_pt_text)
            sheet.write(6,0,numele_pt_text)
            sheet.write(7,0,numele_pt_text)
            sheet.write(8,0,numele_pt_text)
            sheet.write(9,0,numele_pt_text)
            wbk.save("C:/Users/s/Desktop/text/"+numarul_folderelor+".xls")

我会努力的,但我会非常感谢任何帮助。

4

1 回答 1

2

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

    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()
于 2012-10-03T08:10:44.343 回答