[Tutor] write to a file in linux

Tim Wilson wilson@visi.com
Tue, 20 Aug 2002 15:06:27 -0500


On Tue, Aug 20, 2002 at 03:55:48PM -0400, Watt III, Glenn wrote:
> I am trying to write a script in which i write and save a new file in
> linux i ran across tutorial that looks kind of out dated but gave me a
> point in the right direction heres what i have
> 
> >>> import os
> 
> >>> wrt = os.open("an/interesting/filename.py", "w")
> 
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: an integer is required

Try this:

import os
wrt = open("filename", "w")
wrt.write('Here is the text to write.')
wrt.close()

In your example 'wrt' is called a file handle. Any manipulating of the
file occurs by specifying the name of the file handle first (See the
write command above). You'll see a lot of examples that use 'f' as the
file handle name.

-Tim

-- 
Tim Wilson      |   Visit Sibley online:   | Check out:
Henry Sibley HS |  http://www.isd197.org   | http://www.zope.com
W. St. Paul, MN |                          | http://slashdot.org
wilson@visi.com |  <dtml-var pithy_quote>  | http://linux.com