将文件名从文件夹写入CSV [英] Writing filenames from a folder into a csv

查看:163
本文介绍了将文件名从文件夹写入CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析文件夹中的所有文件,并使用Python以CSV格式写入文件名.我使用的代码是

I'm trying to parse all files in a folder and write the filenames in a CSV using Python. The code I used is

import os, csv

f=open("C:/Users/Amber/weights.csv",'r+')
w=csv.writer(f)
for path, dirs, files in os.walk("C:/Users/Amber/Creator"):
    for filename in files:
        w.writerow(filename)

我在CSV中得到的结果是在一列中包含单个字母,而不是整个行名称.该如何解决?

The result I'm getting in the CSV has individual alphabets in one column rather than the entire row name. How to fix that?

推荐答案

import os, csv

f=open("C:/Users/Amber/weights.csv",'r+')
w=csv.writer(f)
for path, dirs, files in os.walk("C:/Users/Amber/Creator"):
    for filename in files:
        w.writerow([filename])

这篇关于将文件名从文件夹写入CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆