Python Program to demonstrate file operations like read, append, write.

#Python Program to demonstrate file operations like read, append, write.

readWRITE = 'w+'
READ = 'r'
Append = 'a'

list0 = [] 

def create():
    file = input("Enter name for a file: ")
    filename = (file+".txt")

    f = open(filename, readWRITE)
    N = int(input("Enter number of lines you want to print:"))
    for i in range (N):
        data = input("%d:"% i)
        list0.append(data)
       
    for i in range(N):
        f.write(list0[i]+"\n")

    f.close()
    print("File Written successfully.")

def read():
    filename = input("\nEnter name of a file with extension that you want to open:")
    print("\ncontent of the file is:\n")
    f = open(filename, READ)
    print (f.read())

def APPEND():
    name = input("\nEnter name of existing file with extension that you want to update:")
    f = open(name, Append)
    N = int(input("Enter Number of lines that you want to add:"))
    list1 = []
    for i in range (N):
        data = input("%d:"% i)
        list1.append(data)
    for j in range (N):
        f.write(list1[j]+"\n")

    f.close()
    print("\nFile updated sucessfully.")


create()
read()
APPEND()
read()

Comments

Popular posts from this blog

Python program to calculate age and year when user will be 100 years old