[Tutor] New to Python

Jeremy Traurig jeremy.traurig at gmail.com
Fri May 18 08:45:51 CEST 2012


Hello,

I very new to python and have been playing around with some simple code
that I would eventually use in the real word. Below is the code I have
created. I want the code to read data from a file called SIL633.txt and
then output that data into another file called test.txt. below is my code:

#! /usr/bin/env python
# Read NRG Text File
def readNRGtxt():
import numpy as np
f1 = open('SIL633_original.txt','r')
data = np.genfromtxt(f1,delimiter='\t',skip_header=141)
f1.close
f2 = open('test.txt','w')
np.savetxt(f2,data,fmt='%6.2f',delimiter='\t')
f2.close

I'm running this on mac 10.5.8. When I run this script from the command
line, there is no file output. However, if I remove the "def" statement in
the script and run again from the command line, a test.txt file is output.
I guess I'm trying to understand why using the def statement changes how
the script operates and since it does, how do I correct this code so it
runs from the command line with the def statement.

thanks for any help -- jeremy
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120517/4e6af6eb/attachment.html>


More information about the Tutor mailing list