简体   繁体   English

在bash / python中循环以调用包含名称固定的命令

[英]loop in bash/python to call a command including name fixation

I have a line of code that works on a single file: 我有一行适用于单个文件的代码:

sextractor ABC347583_ima.fits -CATALOG_NAME ABC347583_ima.cat

this piece of line takes the .fits file and creates a catalog file with the same name but .cat extension 这行代码使用.fits文件,并创建一个具有相同名称但扩展名为.cat的目录文件

Now, I have over 100+ .fits files(All my files start with the name ABC then numbers and end with _ima) and I would like to make a bash/python script that will read the .fits files 1 by 1 and execute the above code with the corresponding file names as input & output. 现在,我有100多个.fits文件(我的所有文件都以名称ABC开头,然后以数字开头,以_ima结尾),我想制作一个bash / python脚本,该脚本将按1读取.fits文件并执行以上代码以及相应的文件名作为输入和输出。

Basicly ABC347583_ima.fits , ABC57334_ima.fits etc. will enter and ABC347583_ima.cat , ABC57334_ima.cat etc. will be created. 基本上,将输入ABC347583_ima.fits,ABC57334_ima.cats等,并创建ABC347583_ima.cat,ABC57334_ima.cat等。

This is beyond my limited knowlegde and what I only know about this is to use the code with 这超出了我的有限知识,而我对此唯一了解的是将代码与

for i in `cat files`
echo $i

However, this does not exactly match with the command line because of both input & output. 但是,由于输入和输出都与命令行不完全匹配。 Any suggestions about how to pass this will be appreciated. 任何有关如何通过此建议将不胜感激。

To iterate in python over all files in a dictonary use os.listdir(). 要在python中遍历字典中的所有文件,请使用os.listdir()。

Then you can loop over the filenames. 然后,您可以遍历文件名。 in the "callthecommandhere" function you can parse the filenames and read the file content and write a new file. 在“ callthecommandhere”函数中,您可以解析文件名并读取文件内容并写入新文件。 I hope I understand you right and that is a help for you. 希望我对您的理解正确,对您有所帮助。

Like so: 像这样:

import os
for filename in os.listdir('dirname'):
     callthecommandhere(blablahbla, filename, foo)

Br christoph 克里斯托夫(Br Christophh)

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

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