[Tutor] Pass arguments from bash script to embedded python script
David L Neil
PyTutor at DancesWithMice.info
Fri Oct 25 18:45:31 EDT 2019
On 26/10/19 12:24 AM, Stephen P. Molnar wrote:
> Thanks to the help from this list, I have achieved success. Here is my
> code:
>
>> #!/usr/bin/env python3
>> # -*- coding: utf-8 -*-
>> """
>> Created on Fri Oct 11 09:36:30 2019
>>
>> """
>>
>> import os
>> import glob
>> import numpy as np
>>
>> fileList = []
>>
>>
>> for files in glob.glob("*.log"):
>> fileName, fileExtension = os.path.splitext(files)
>> fileList.append(fileName)
>>
>> for filename in fileList:
>> data = np.genfromtxt(filename+'.log', usecols=(1), skip_header=27,
>> skip_footer=1, encoding=None)
>> np.savetxt(filename+'-dG', data, fmt='%.15g', header=filename)
>
> Many, many thanks.
As @Mats has said: it's working so why mess with it!
For learning interest:
Examine the two loops. What are their purposes?
Consider these two lines:
>> fileList.append(fileName)
>>
>> for filename in fileList:
We start with fileName and list it. Then we take the list and process
each element. Could the two loops be compressed into one?
Answer: split the fileName and pass the result directly into the np
operations.
NB I can understand why one might prefer the two-step approach, if
that's the way your mind breaks-down the problem, ie shorter code is not
necessarily 'better' code!
Thanks for the personal note. Previous attempts to respond were blocked
by SBC's email filter. I do have a (non-Python) question I'd like to
pose but would be grossly OT 'here'...
--
Regards =dn
More information about the Tutor
mailing list