#TeCoEd @dan_aldred
#Writing data to a text file
#12/04/2013
import time

# def, opens the file called 'test' in write mode w
# for each of the items i, in the list it writes them to the file with a new line \n
# close the file called test .close

list = []

# adds input to a global list and writes to file 
def write_to_file():
    global list
    data_to_add()
    print " "
    file_name = raw_input("Please enter the name of the file you are creating: ")
    file = open(file_name + ".txt", "w") #test.txt is the name of the text file the program writes to
    for i in list:
        file.write(i+'\n')
    file.close()
    print "Your data has been added"
    print " "
    time.sleep(1)
    
def data_to_add():
    global list
    list = []
    times = input("How many lines of data do you wish to add to the file? ")
    for i in range(times): 
        file_text = raw_input("Please enter your line of data: ")
        list.append(file_text) #adds the data to your list
        print list #checks the data has been added to the list
    return list
    
def read_from_file():
    file_name = raw_input("Please enter the name of the file you wish to read: ")
    #allows user to select the text file that they want to be read
    print " "
    file_obj = open(file_name + ".txt")#opens file
    x = file_obj.read()#reads file contents
    print "The contents of the file are", x #prints the contents of the text file
    time.sleep(2)

def delete_from_file():
    file_name = raw_input("Please enter the name of the file you wish to delete: ")
    file = open(file_name + ".txt", "w") #test.txt is the name of the text file the program writes to
    x = " "
    print " "
    file.write(x)
    file.close()
    print "Your data has been deleted"
    print " "
    time.sleep(2)

def start():
    answer = raw_input("Please select: \nR to read from a file, \nW to write to a file \nD to delete a file \nor Q to quit \nYour selection: ").upper()
    print " "
    if answer == "R":
        read_from_file()
    elif answer == "W":
        write_to_file()
    elif answer == "D":
        delete_from_file()
    #elif answer == "Q":
        #break
    start()   

start()         
         


