[Tutor] Pass arguments from bash script to embedded python script
David
bouncingcats at gmail.com
Wed Oct 23 17:41:43 EDT 2019
On Thu, 24 Oct 2019 at 06:08, Stephen P. Molnar <s.molnar at sbcglobal.net> wrote:
>
> I have revised my script [...]
Hi Stephen,
That's great progress! Not much to fix, almost done!
First, here's a runnable demo of your biggest mistake:
(I'm using the same shebang line as you showed, remove the
"cut here" comments and run it).
#----cut here----
#!/usr/bin/env python
fileList = ['C-VX3', '15-7', '14-7']
fname = fileList
for fname in fname:
print('Inside the for statement', fname)
print('After the for statement', fname)
#----cut here----
I hope that demonstrates that all the work that you want to do
with the iterated variables in a 'for' statement must appear inside
(indented directly below) its parent for statement.
You can use functions (def) to create a clean structure, here's
another similar runnable demo.
#----cut here----
#~ #!/usr/bin/env python
def doubler(x):
return 2 * x
for a in ['a', 2, 'b']:
b = doubler(a)
print('Inside the for statement:', b)
print('After the for statement', b)
#----cut here----
Also, even though it does not error, this code that you showed
fname = fileList
for fname in fname:
fname
is bad because:
1) It unnecessarily re-uses fname for two purposes
(which works but requires unnecessary mental effort for everyone).
2) The body of the for statement (the fname statement) does nothing.
It should be replaced by this:
for fname in fileList:
# do something with fname here
# for example ...
print(doubler(fname))
More information about the Tutor
mailing list