#file_write_example_1.py
import random
import sys
import os

test_file = open("test.txt", "w")


print("\nfile to write in is : ",test_file.name)
print("\nfile is opened in mode = : ",test_file.mode)
print("""\nThis mode creates the file if it does not exist
and permits writing to the file """)

test_file.write("Write me to the file")

test_file.close()

test_file =open("test.txt", "r+")
print("\nfile is opened in mode = : ",test_file.mode)
print("""\nThis mode permits reading from the file
and writing to the file""")

text_in_file = test_file.read()
print("\nThe following line was read from the file : ")
print("                   ",text_in_file,"\n")
test_file.close()  #could not remove while still open

os.remove("test.txt")  # deletes the file

##  informatin below is from
##      http://stackoverflow.com/questions/1466000/python-open-built-in-function-difference-between-modes-a-a-w-w-and-r

## The argument mode points to a string beginning with one of the following
## sequences (Additional characters may follow these sequences.):
##
## ``r''   Open text file for reading.  The stream is positioned at the
##         beginning of the file.
##
## ``r+''  Open for reading and writing.  The stream is positioned at the
##         beginning of the file.
##
## ``w''   Truncate file to zero length or create text file for writing.
##         The stream is positioned at the beginning of the file.
##
## ``w+''  Open for reading and writing.  The file is created if it does not
##         exist, otherwise it is truncated.  The stream is positioned at
##         the beginning of the file.
##
## ``a''   Open for writing.  The file is created if it does not exist.  The
##         stream is positioned at the end of the file.  Subsequent writes
##         to the file will always end up at the then current end of file,
##         irrespective of any intervening fseek(3) or similar.
##
## ``a+''  Open for reading and writing.  The file is created if it does not
##         exist.  The stream is positioned at the end of the file.  Subse-
##         quent writes to the file will always end up at the then current
##         end of file, irrespective of any intervening fseek(3) or similar.


