繁体   English   中英

如何存储目录中的所有文件名并保存在excel中?

[英]How to store all file names from the directory and save it in excel?

各位晚上好。

我写了一小段代码(从这个stackoverflow搜索获得帮助)

我可以从目录中获取文件列表。

我试图存储在 Excel 电子表格中。 我只能在 excel 中存储文件的两行,但不能存储所有文件名。

请检查代码和 output。

帮我一个解决方案,列出 excel 中的所有文件名。

太感谢了。

代码:-

import os
import pandas as pd
path = "//home//halovivek//Downloads//education//Jimi Kwik - Super Brain//"
list = []
for (root,dirs, file) in os.walk(path):
    for f in file:
        print(f)

my_data = pd.DataFrame(file)
my_data.to_excel("outputfile.xlsx", index = False, header= False)

output:-

/home/halovivek/PycharmProjects/yearcoding/venv/bin/python /home/halovivek/PycharmProjects/yearcoding/26082002_listfilesfromdirectory.py 第 12 天 - 实施日 - 杂耍练习.mp4 第 12 天 - 实施日 - 杂耍练习.MP3 第 2 部分 - 准备For The Quest.mp4 第 1 部分 - 欢迎来到您的 30 天超级大脑 Quest.mp4 第 5 部分

  • 天才们用来启动大脑的 10 个早晨习惯。mp4 第 3 部分 - 学习任何东西的快速方法。mp4 第 2 部分 - 为 Quest.MP3 第 1 部分做准备 - 欢迎来到您的 30 天超级大脑 Quest.MP3 第 5 部分
  • 天才们用来启动大脑的 10 个早晨习惯。MP3 第 4 部分 - 如何记笔记。mp4 第 3 部分 - 学习任何东西的快速方法。MP3 第 4 部分 - 如何记笔记。MP3 第 27 天 - Memory 的古代字母数字代码第 2 部分 - 应用程序.MP3 第 27 天 - Memory 的古代字母数字代码第 2 部分 - 应用程序.mp4 第 29 天 - 转换的 5 个级别。MP3 第 29 天 - 转换的 5 个级别.mp4 第 14 天 - Z4789F23283B3A61F858B641A1 .MP3 第 14 天 - Memory 就像 PIE 一样简单。mp4 第 15 天 - FDR 技术.MP3 第 15 天 - FDR 技术.mp4 第 31 天 - 克服拖延症.mp4 第 31 天 - 克服拖延症.MP3 第 9 天 - 链式链接 - 部分1.mp4 第 9 天 - 链接 - 第 1.MP3 第 8 天 - 实施日 - 早上例程.mp4 第 8 天 - 实施日 - 早上例程.MP3 第 24 天 - 实施日 - 交叉。MP3 第 24 天 - 实施日 - 交叉。 mp4 日 19 - 学习外语.mp4 第 19 天 - 学习外语.MP3 第 13 天 - 温文尔雅记住名字.MP3 第 13 天 - 温文尔雅记住名字.mp4 第 34 天 - 速读.MP3 第 34 天 - 速读.mp4 第 22 天 -位置方法.MP3 第 22 天 - 位置方法.mp4 第 5 天 - 营养 _ 你的身体 Folders.MP3 第 5 天 - 营养 _ 你的身体 Folders.mp4 第 23 天 - 逐字背诵. MP3 第 23 天 - 逐字背诵。 mp4 第 30 天 - 与 Jim.mp4 的问答 Session 第 30 天 - 学习的 5 个级别.mp4 第 30 天 - 学习的 5 个级别。MP3 第 30 天 - 与 Jim.MP3 的问答 第 28 天 - 实施日 - 音标代码.MP3 天28 - Implementation Day - Phonetic Number Code.mp4 How To Become A Super Learner Masterclass - Jim Kwik.mp4 how_to_become_a_super_learner_by_jim_kwik_workbook_nsp.pdf Day 18 - Keyword Substitution Method.MP3 Day 18 - Keyword Substitution Method.mp4 Day 32 - Your 8 C's To Muscle Memory .m p4 第 32 天 - 你的 8 C's To Muscle Memory.MP3 第 4 天 - 实施日 - 间隔重复概念.mp4 第 4 天 - 实施日 - 间隔重复概念.MP3 第 26 天 - Z4789F23283B3A61F39AZB641A1BEF 的古代字母数字代码。第 26 天 - Memory 的古代字母数字代码第 1 部分
  • The Sounds.mp4 第 2 天 - 太阳升起.mp4 第 2 天 - 太阳升起.MP3 第 16 天 - 实施日 - Superbrain Yoga.MP3 第 16 天 - 实施日 - Superbrain Yoga.mp4 第 10 天 - 链式链接 - 第 2 部分.MP3 第 10 天 - 链接 - 第 2 部分.mp4.getxfer.14810.259.mega 第 7 天 - 睡眠_压力管理.MP3 第 7 天 - 睡眠_压力管理.mp4 第 33 天 - 记住你的梦想.mp4 第 33 天 - 记住你的梦想.MP3.getxfer.14709.10.mega.getxfer.28947.5.mega 第 21 天 - 如何在没有笔记的情况下发表演讲。MP3.getxfer.27388.5.mega 第 3 天 - 解锁超级大脑的 10 把钥匙.mp4.getxfer.10040.5.mega第 6 天 - 环境 _ Killing ANTs.MP3 _Groupinsiders.com.url 第 6 天 - 环境 _ Killing ANTs.mp4 第 25 天 - 数字 - 基础知识.mp4 第 25 天 - 数字 - 基本知识。MP4 第 25 天 - 数字 - 基本知识MP3 第 1 天 - MOM 可以帮你记住 unting To 10 In Japanese.mp4 Day 11 - The Peg Memory Method.MP3 Day 11 - The Peg Memory Method.mp4 Day 17 - The Ultimate TIP To remembering Anything.MP3 Day 17 - The Ultimate TIP To remembering Anything.mp4

进程以退出代码 0 结束

在此处输入图像描述

你的问题在这里......'list'是python中的一个关键字......所以当你尝试声明一个列表时不能用作变量名......你实际上并没有对你的循环中的值做任何事情Glob 更容易处理目录

import glob
list_of_mp3s_mp4s = []
for file_or_dir in glob("//home//halovivek//Downloads//education//Jimi Kwik - Super Brain//**", recursive = True):
        if os.path.isfile(file_or_dir) and file_or_dir.lower().__contains__(".mp3") or file_or_dir.lower().__contains__(".mp4"):
                list_of_mp3s_mp4s.append(file_or_dir)
my_data = pd.DataFrame(list_of_mp3s)
my_data.to_excel("outputfile.xlsx", index = False, header= False)
               

import os
import pandas as pd
#path of the file you want to enemurate
path = "//home//halovivek//Downloads//"
directory =[]
filename=[]

for (root,dirs, file) in os.walk(path):
    for f in file:
        directory.append(root)
        filename.append(f)
        print(f)

#column name of the sheet
df=pd.DataFrame(list(zip(directory,filename)),columns=['Directory',"filename"])
#change the file of exccl sheet
df.to_csv("all.csv")

它将列出所有文件并将其以 csv 格式存储在电子表格中。 我测试了它的工作正常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM