Writing Files
eharetea

Writing to a file.

Write files in Python.

Date Created:Friday December 29th, 2006 03:41 AM
Date Modified:Friday August 01st, 2008 02:07 AM

# writes a string to a file:

>>> f=open('newfile.txt','w')
>>> f.write('Text here!')
>>> f.close()
>>> open('newfile.txt').read()
'Text here!'



# uses the tr unix command to capitalize text

>>> import pipes
>>> t=pipes.Template()
# the append Template object appends a new action at the end
# the first argument must be a bourne shell command
# the second argument is saying read as standard input, write as standard output
>>> t.append('tr a-z A-Z', '--')
>>> f=t.open('readme.txt', 'w' )
>>> f.write('hello world')
>>> f.close()
>>> open('readme.txt').read()
'HELLO WORLD'