By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,720 Members | 2,135 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,720 IT Pros & Developers. It's quick & easy.

How to count total number of water ('HOH') molecules in each file ?

P: 6
#!/usr/bin/python
import os
path=os.getcwd()
print path
list_of_filenames=os.listdir(path+'//newfiles')
print list_of_filenames
residue=[]
for f in list_of_filenames:
f1=open(path+'//newfiles//'+f).readlines()
for line in f1:
if line.startswith('HETATM'):
res_number=line[22:26]
if res_number not in residue and line[17:20]=='HOH':
residue.append(res_number)
else:
continue
else:
continue
print(len(residue))



As the output of this script I got only one value which is the total number of 'HOH' molecules in all the files in that directory. But I need to count total number of 'HOH' molecules in each file one by one.
So please can you help me to find out how this script should be changed according to my requirement?
Mar 12 '17 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 621
Expand|Select|Wrap|Line Numbers
  1. for f in list_of_filenames:
  2.     residue=[]  ## new list for each name
  3.     f1=open(patf1=open(path+'//newfiles//'+f).readlines()
  4.     for line in f1:
  5.         ...
  6.     print(len(residue))  ## for each name 
And note that else + continue does nothing. Try it for yourself and see what happens.
Mar 12 '17 #2

P: 6
@dwblas Thank you. I will try.
Mar 13 '17 #3

Post your reply

Sign in to post your reply or Sign up for a free account.