简体   繁体   English

如何使用bash遍历命令以获取目录中文件数?

[英]How to loop through the command for number of files in directory using bash?

Here iam using a bash script which runs like this 我在这里使用运行这样的bash脚本

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

Here iam downloading some files to my directory name folder_name then i want to run the script taking arguments as each and every file name.I tried to use this logic but it failed giving me this error. 在这里我将一些文件下载到我的目录名folder_name然后我想运行以每个文件名作为参数的脚本。我尝试使用此逻辑,但失败给了我这个错误。

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

Is there any way to repeat the loop taking each file as argument and passing it to the python file. 有没有办法重复循环,将每个文件作为参数并将其传递给python文件。

Presumably, you want to iterate over the files in a directory and run some python script taking each filename as argument; 大概您想遍历目录中的文件,并运行一个以每个文件名作为参数的python脚本。 if so, just globbing to get the filenames would do: 如果是这样,只需遍历获取文件名即可:

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

Replace /directory/ with the actual directory name. 用实际的目录名称替换/directory/

os.listdir() is a function from os module in python that returns a list containing the contents of a directory as strings, the same thing you can achieve with simple shell globbing in any shell. os.listdir()python os模块中的一个函数,该函数以字符串形式返回包含目录内容的列表,这与在任何shell中进行简单的shell遍历操作都可以实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM