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

时间:2012-10-03 07:09:17

标签: python file-io directory subdirectory

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

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")

我会继续努力,但我会非常关心任何帮助。

1 个答案:

答案 0 :(得分: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()