Friday, 15 June 2018

To read and write from a file using python programmin.

# To read and write from a file

str = input("Enter file name to open : ")


while(True):
    print("Choose one option : ")
    print("1. Write to file.")
    print("2. Read from file")
    print("e. exit")

    op = input();
    if op == '1':
        file = open(str+".txt","w")
        ele = input("Enter a string to write to file : ")
        file.write(ele+" ")
        file.close()
    elif op =='2':
        try:
            file = open(str+".txt","r")
            fd = file.read()
            print("file data : ")
            print(fd)
            file.close()
        except (FileNotFoundError):
            print("File not found.")
    elif op == 'e':
        break
    else:
        print("Choose correct option.")

No comments:

Post a Comment

Featured post

In all other cases the driver is not insured. If the marital status, sex and age of the driver are the inputs, write a program to determine whether the driver is to be insured or not.

  #include #include int main() { char ms;   printf("Is Driver married (Y/N): ");  scanf("%c",&ms);   if(ms=='y&#...

Popular Posts