Launch a python program from a shell script

-  
Hello community,

I have had a problem for a while that seems tiny, yet I can't find a solution despite my long searches (on forums, etc...). I must say that I'm new to Linux commands...

I created a Python program that takes a .dat data file as input. I can run it without any issues via the console using the command "python3 my_program.py my_file.dat".

Now, I need to create a shell script (or a Python program, but I've tried extensively with shell script) that executes my first program as many times as there are data files containing the word "NOEUD" in the current directory.

Here's what my code looks like:

for file in find -type f -name "NOEUD*.dat"/my_directory/*
do
python3 my_program.py file
done

This generates the error:
FileNotFoundError: [Errno 2] No such file or directory: 'file'

Basically, it seems that the Python program is starting correctly, but its input parameter (which should be a .dat data file) does not recognize that the variable "file" in the shell script is a file. And the second problem is that the error message appears 5 times (as if the program was trying to run 5 times), even though there aren’t 5 files named "NOEUD*.dat"...

Thank you in advance if someone can help!

4 answers

  1. Contributor
    Hello,

    Before launching anything, we generally start by seeing what a command might return…

    So personally, I would start by checking what the command returns (also check the man find for the correct syntax ;-\) :
    find /my_directory/ -type f -name "NODE*.dat"

    If the return is conclusive, with the examples at the end of the man find, I would try to include the Python program…

    --
    A penguin on the ice.
    0
    1. When testing the command you provided, I get back the 3 files that correspond to the search, so that's very good; so far, everything is working correctly.

      Then I integrate it into the program:

      for file in find /my_directory/ -type f -name "NOEUD*.dat"
      do
      python3 my_prog.py file
      done

      And there I get the same error... But this time it appears 6 times instead of 5...

      Could the problem be coming from my loop where I write "for file in find ... ? Since the find ... command returns the names of the files that match the request, that seems possible to me...
      0
      1. Contributor
        No need for a loop, the
        find
        command with its options (notably
        exec
        ) or through a pipe to the
        xargs
        command should be sufficient.
        0
    2. Hello,

      keep going with Python.

      You are capable of writing a Python script that processes a file, but you don't know how to insert a loop to process multiple ones?! 8O

      You've done the hardest part; inserting a loop in a Python program seems trivial to me.
      But I don't do Python. :/

      And then
      find
      to find files in the current directory is useless.
      0
      1. lEprofSonDkon, to be honest, I initially started by trying to do it in Python, but I ran into "surprising" problems that made me decide to write it instead in a shell script. And now, the script is literally 3 lines long so I'm convinced that it's really an even more trivial problem than in Python... I really want to succeed in shell script... But indeed, if I fail again, I will try again with Python.
        0
        1. I came across some "surprising" problems

          Can you elaborate, please?
          0
        2. Well, I finally got my "head back into Python." I did it differently and I succeeded. I would have liked to achieve it in shell script as well, but well, when it doesn’t work, it doesn’t work...

          Thank you all!
          0
        3. @pasletempsdetrouverunpseudoit's really simple in shell:
          for f in /myDirectory/NODE*.dat; do test -f "$f" && echo myPRg.py "$f"; done

          but I insist, it's better to do it in the python script.
          0
        4. For your problem, you need to put $file in your loop
          do
          python3 my_program.py file
          done

          Because you are looking for all the files named NOEUD*.dat.
          So "file" is a variable.
          And variables are "called" with a dollar sign preceding them.
          Trauqnej
          0
        5. @trauqnejit won't be enough; the
          find
          should also be within a Command substitution
          but it's bad practice to parse a command substitution with
          for
          .
          0