Launch a python program from a shell script
pasletempsdetrouverunpseudo
Posted messages
4
Status
Membre
-
lEprofSonDkon Posted messages 227 Status Membre -
lEprofSonDkon Posted messages 227 Status Membre -
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!
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 réponses
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 ;-\) :
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.
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.
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...
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...
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
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
findto find files in the current directory is useless.
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.