Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #70556 > unrolled thread

Re: how to write list in a file

Started byTerry Reedy <tjreedy@udel.edu>
First post2014-04-23 19:50 -0400
Last post2014-04-23 19:50 -0400
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: how to write list in a file Terry Reedy <tjreedy@udel.edu> - 2014-04-23 19:50 -0400

#70556 — Re: how to write list in a file

FromTerry Reedy <tjreedy@udel.edu>
Date2014-04-23 19:50 -0400
SubjectRe: how to write list in a file
Message-ID<mailman.9474.1398297087.18130.python-list@python.org>
On 4/23/2014 3:53 AM, Dhananjay wrote:
> Hello everyone,
>
> I am trying hard to write a list to a file as follows:
>
>
> def average_ELECT(pwd):
>      os.chdir(pwd)

I would 'print pwd' to make sure where files are being opened.

>      files = filter(os.path.isfile, os.listdir('./'))
>      folders = filter(os.path.isdir, os.listdir('./'))
>      eelec = 0.0; evdw = 0.0; EELEC = []; elecutoff = [];
>      g = Gnuplot.Gnuplot()
>      for f1 in files:
> #       if f1[21:23]=='12':
>          if f1[27:29]==sys.argv[1]: # vdw cutoff remains constant; see
> 2nd column of output
>              fl1 = open(f1,'r').readlines()
>      #       print len(fl1)
>
>              for i in range(1, len(fl1)):
>                  fl1[i]=fl1[i].split()
>                  eelec = eelec + float(fl1[i][1])
>                  evdw = evdw + float(fl1[i][2])
>                  #print fl1[i][1], fl1[i][2]
>              avg_eelec = eelec/40
>              avg_evdw = evdw/40
>      #       print eelec, evdw
>      #       print f1[21:23], f1[27:29], avg_eelec, avg_evdw
>              print f1[21:23], f1[27:29], avg_eelec
>      #       EELEC.append(avg_eelec); elecutoff.append(float(f1[21:23]))
>              eelec=0.0; evde=0.0;
>              a = f1[21:23]+' '+f1[27:29]+' '+str(avg_eelec)
>              EELEC.append(a)
>      print sorted(EELEC)
>      with open('EElect_elec12-40_vdwxxx.dat','w') as wr:
>          for i in EELEC:
>              print i
>              wr.write("%s\n" % i)
>      wr.close()
>
>
> The script is printing "print sorted(EELEC)" as well as "print
> f1[21:23], f1[27:29], avg_eelec" very well.
> However, for some reason, I neither see any file (expected to see
> EElect_elec12-40_vdwxxx.dat as per the script) generated nor any error
> message.
>
> Could anyone suggest me correction here.
>
> Thanking you in advance.
>
> -- DJ
>
>
>
>


-- 
Terry Jan Reedy

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web