[newbie] Writing to file : "name 'concat' is not defined"?

Byron DesertLinux at netscape.net
Wed Jul 21 17:49:28 CEST 2004


Hi Fred,

It looks like they have answered your questions.  To reiterate in a 
simple form, to open a file for reading, it is best to use something 
like the following:

	f = open("c:/test.txt", "r")
	textlines = f.readlines()			# Reads all lines into a list (array).
	for line in textlines:				     # Prints each line that is read from the 
text file.
		print line

	---

If you want to append (add additional information) to a text file, use 
the following:

	f = open("c:/test.txt", "a")
	f.write("This is an appended line.\n")
	f.close()


Hope this helps,

Byron
------------------------



Fred wrote:
> Hi,
> 
> 	I've searched the web and the archives of this ng for half an
> hour already, and I'm still stuck.
> 
> 1. On a W2K Pro host, installed the latest ActivePython MSI. Rebooted.
> 2. Create test.py with the following:
> 
> file=concat('test.txt','w')
> file.close()	
> 
> 3. Open a DOS box, and type either "test.py" or "python test.py":
> 
> "NameError: name 'concat' is not defined"
> 
> I tried the following:
> 
> - from operator import *  -> "AttributeError: 'str' object has no
> attribute 'close'"
> - import operator -> "NameError: name 'concat' is not defined"
> - file=concat('./test.txt','w')
> - file=concat('.\test.txt','w')
> 
> ... all to no avail. Obviously, I'm either missing a package or I need
> to tweak something. Any idea?
> 
> Thank you
> Fred.



More information about the Python-list mailing list