【问题标题】:How to loop through the command for number of files in directory using bash?如何使用bash遍历目录中文件数的命令?
【发布时间】:2017-06-05 01:03:48
【问题描述】:

这里我使用的是这样运行的 bash 脚本

for d in os.listdir($folder_name); do
        echo "Running the scanner :"
        python bd.py $folder_name/$d
done

我在这里将一些文件下载到我的目录名folder_name 然后我想运行脚本,将参数作为每个文件名。我尝试使用这个逻辑,但它没有给我这个错误。

./test: line 29: syntax error near unexpected token `('
./test: line 29: `for d in os.listdir($folder_name); do'

有没有办法重复循环,将每个文件作为参数并将其传递给 python 文件。

【问题讨论】:

  • 我没有看到任何看起来像 Python 的 Bash 脚本。
  • 好吧,既然你已经删除了import os...你还有os.listdir,看起来像Python。
  • 对我来说仍然看起来很奇怪
  • 对不起,我不明白。
  • SaiKiran:参考下面@heemayl 的逻辑,这正是你所需要的。

标签: python bash syntax operating-system


【解决方案1】:

大概,您想遍历目录中的文件并运行一些python 脚本,将每个文件名作为参数;如果是这样,只需 globbing 获取文件名即可:

for f in /directory/*; do python bd.py "$f"; done

将/directory/ 替换为实际的目录名称。

os.listdir() 是 python 中 os 模块中的一个函数,它返回一个包含目录内容的列表作为字符串,这与在任何 shell 中通过简单的 shell globbing 可以实现的效果相同。

【讨论】:

    猜你喜欢
    • 2014-10-02
    • 1970-01-01
    • 2021-12-18
    • 2010-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-29
    • 1970-01-01
    相关资源
    最近更新 更多