在python文件夹中的文件上使用选项运行.py

2024-04-20 01:03:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个python脚本,在mac终端上使用以下命令在文本文件上运行:

python texter.py --text file.txt

如果要在文本文件文件夹上迭代脚本,请在mac termianl上运行以下命令:

bash for f in ~/directory; do python texter.py --text $file done;

但是,我得到以下错误:

-bash: syntax error near unexpected token `do'

第二行代码是否遗漏了什么?或者有没有其他更好的方法来管理文件夹中的文件?你知道吗


Tags: textpy命令txt脚本文件夹bash终端
2条回答

总结评论中的答案

  • 如果你想循环浏览文件,你需要dir/*
  • 如果要使其成为单行命令,则需要在正确的位置添加分号

所以呢

for file in ~/directory/* ; do python texter.py  text $file; done

会和

for file in ~/directory/* 
do 
   python texter.py  text $file
done 

这应该起作用:

for file in ~/directory/ do python texter.py  text $file; done (note the ; before done) 

相关问题 更多 >