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


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

how to write list in a file

Started byDhananjay <dhananjay.c.joshi@gmail.com>
First post2014-04-23 15:53 +0800
Last post2014-04-23 15:53 +0800
Articles 1 — 1 participant

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


Contents

  how to write list in a file Dhananjay <dhananjay.c.joshi@gmail.com> - 2014-04-23 15:53 +0800

#70539 — how to write list in a file

FromDhananjay <dhananjay.c.joshi@gmail.com>
Date2014-04-23 15:53 +0800
Subjecthow to write list in a file
Message-ID<mailman.9461.1398239639.18130.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

Hello everyone,

I am trying hard to write a list to a file as follows:


def average_ELECT(pwd):
    os.chdir(pwd)
    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

[toc] | [standalone]


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


csiph-web