对目录中的所有文件运行 python 脚本 [英] Running a python script on all the files in a directory

查看:31
本文介绍了对目录中的所有文件运行 python 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Python 脚本,它可以读取一个文本 csv 文件并创建一个播放列表文件.但是我一次只能做一个,比如:

I have a Python script that reads through a text csv file and creates a playlist file. However I can only do one at a time, like:

python playlist.py foo.csv foolist.txt

但是,我有一个需要制作成播放列表的文件目录,名称不同,有时文件数量也不同.

However, I have a directory of files that need to be made into a playlist, with different names, and sometimes a different number of files.

到目前为止,我已经创建了一个 txt 文件,其中列出了目录中文件的所有名称,然后循环遍历其中的每一行,但是我知道必须有更简单的方法来做到这一点.

So far I have looked at creating a txt file with a list of all the names of the file in the directory, then loop through each line of that, however I know there must be an easier way to do it.

推荐答案

for f in *.csv; do
  python playlist.py "$f" "${f%.csv}list.txt"
done

这能解决问题吗?这将把 foo.csv 放在愚蠢的.txt 中,将 abc.csv 放在 abclist.txt 中.

Will that do the trick? This will put foo.csv in foolist.txt and abc.csv in abclist.txt.

还是希望它们都在同一个文件中?

Or do you want them all in the same file?

这篇关于对目录中的所有文件运行 python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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