【问题标题】:filenames, directory, text, python folders subfolders文件名、目录、文本、python 文件夹子文件夹
【发布时间】:2012-10-03 07:09:17
【问题描述】:

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

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

【问题讨论】:

  • 您的代码在我的电脑上运行良好..
  • 您应该以追加模式而不是写入模式打开文件。在写入模式下打开已经存在的文件会删除旧内容,因此每次您将新文件名写入文本文件时,都会删除旧名称并写入新名称。这导致您在文本文件中只有最后一个文件名。将打开文件的行更改为fisier_text = open(""+root_text+"", "a")
  • @halex:我不介意覆盖它... @Rohit Jain:你说它有效是什么意思?它将每个子文件夹 Desktop/filme/ 中的每个文件(例如:/filme/1/movieoane、movietwo、moviethree./filme/2/movieoane、movietwo、moviethree.)保存在文件 1.txt 中 2.txt 在文件夹 /Desktop /文本/?

标签: python file-io directory subdirectory


【解决方案1】:

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

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

【讨论】:

  • 我重新编辑了问题...我想像文本文件一样保存它,但在 excel 中,我会处理它,但如果有人有解决方案,我会对其进行处理。非常感谢您的帮助。
猜你喜欢
  • 2013-08-20
  • 1970-01-01
  • 2018-07-28
  • 1970-01-01
  • 1970-01-01
  • 2018-05-22
  • 1970-01-01
  • 1970-01-01
  • 2021-11-08
相关资源
最近更新 更多