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

查看:187
本文介绍了在目录中的所有文件上运行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放在foolist.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天全站免登陆