Feedback ? Send it to admin@fullchipdesign.com or join me at fullchip@gmail.com

Program to open and write file
This program writes to the file specified in the python code
import sys
import os
import string
def write_file(file):
file_to_write = open('test_write.txt', 'w')
file_to_write.write('Text to write')
file_to_write.close()
file = write_file(open('test_write.txt', 'w'))
Now lets make the file writes to be more interesting
Code to write into files specified from the command line
#!/usr/bin/python
import sys
import os
import string
def write_file(file):
file_to_write = file
file_to_write.write('Text to write')
file_to_write.close()
return
if len(sys.argv) == 1:
write_file(sys.stdin)
else:
write_file(open(sys.argv[1], 'w'))
We can further modify the writes by using classes and global variables in python
import sys
import os
import string
class writefile:
def write_file(self, file):
self.file_to_write = file
self.file_to_write.write('Text to write')
self.file_to_write.close()
return
if len(sys.argv) == 1:
r1 = writefile()
r1.write_file(sys.stdin)
else:
r1 = writefile()
r1.write_file(open(sys.argv[1], 'w'))
Python file write operations