[Tutor] Pass arguments from bash script to embedded python script

Stephen P. Molnar s.molnar at sbcglobal.net
Wed Oct 23 15:08:07 EDT 2019


I have revised my script to make use of the def function:

#!/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 = []
filesList = []

for files in glob.glob("*.log"):
     fileName, fileExtension = os.path.splitext(files)
     fileList.append(fileName)
     filesList.append(files)


fname = fileList
for fname in fname:
     fname

fname1 = fname+'.log'
fname2 = fname+'-dG'
print('fname = ', fname)
print('fname1 = ',fname)
print('fname2 = ',fname2)

def dG(filesList):
     data = np.genfromtxt(fname1, usecols=(1), skip_header=27, 
skip_footer=1, encoding=None)
     np.savetxt(fname2, data, fmt='%.10g', header=fname)
     return(data)

data = dG(filesList)
print(data)

where fileList = ['C-VX3', '15-7', '14-7']

Note:  the extraneous print statements s are there to track results 
during execution.

The result of running the script:

# 14-7
-9.960902669
-8.979504781
-8.942611364
-8.91552301
-8.736508831
-8.663387139
-8.410739711
-8.389146347
-8.296798909
-8.168454106
-8.127990818
-8.127103774
-7.979090739
-7.941872682
-7.900766215
-7.881485228
-7.837826485
-7.815909505
-7.722540286
-7.720346742

It seems to work with one little (actually major) problem. The only 
result saved is for the last file in the list 14-7.log.

Which s the last file  in the list.

Just what am I doing or not doing?

Thanks in advance.



On 10/21/2019 06:49 PM, David wrote:
> On Tue, 22 Oct 2019 at 00:45, Stephen P. Molnar <s.molnar at sbcglobal.net> wrote:
>
> [...]
>
>> and I have a bash script in which I have embedded the python script:
> Why is the task split between two scripts in two different languages?
> This seems misguided and to introduce unnecessary complications,
> given the simple nature of both scripts that were shown.
>
> [...]
>
>>> while IFS= read -r d
>>> do
>>>      python3 DeltaGTable_V_sw.py
>>> done <ligand.list
> The functionality in that bash code is simple to achieve using Python.
>
> [...]
>
>> The ligand.list for the bash file have the format:
>>>> fname = ['C-VX3.log', '15-7.log', '14-7.log']
>>>> fname1 = ['C-VX3', '15-7', '14-7'
> I wonder what generates that content? Because it strongly
> resembles runnable Python code. See below.
>
> I may be misunderstanding, but using Bash to read files
> containing what looks like Python source code looks like a
> confused design. Is there some reason for that?
>
> It looks like Bash is used to parse data out of Python syntax
> before then passing that data to a Python script. Yes, it could
> be made to work, but it looks like a confused and inefficient design.
>
> A cleaner approach would be to write one Python script that does
> everything required, and avoid all the unnecessary parsing and
> argument passing. If the Bash script was complicated, I wouldn't
> advocate this because it would be a lot of work. But the Bash
> script you showed us is trivial.
>
>> So my question is what question should I be goggling? I am not looking
>> for someone to write the script for me, but pointers in the correct
>> direction.
> Possible approaches:
>
> 1) In Python, how to read a file line-by-line, and parse data out of it.
> If that is actually needed? ...
>
> 2) Or, if it is not a security risk and you control the file content, you might
> be able to directly apply Python's exec() function [1] directly to the
> entire content of your ligand.list file. In that case there would be no
> need to read the ligand.list file line-by-line.
>
> [1] https://docs.python.org/3/library/functions.html#exec
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>

-- 
Stephen P. Molnar, Ph.D.
www.molecular-modeling.net
614.312.7528 (c)
Skype:  smolnar1



More information about the Tutor mailing list