File Handling

Total Post:113

Points:791
 157  View(s)
Ratings:
Rate this:

File Handling and read-write in Python ?

How to use of read(), write(), delete() ?

  1. Post:140

    Points:984
    Re: File Handling

    File Handling:

    All web application are having a important part its called File Handling. Python has several  functions for creating,reading,updating, deleting files. 

    Syntax :

    f = open("demofile.txt") 

    Example :

    f = open("demofile.txt", "r") 
    print(f.read())

    Write to an Existing File: 

    f = open("demofile.txt", "w") 
    f.write("Woops! I have deleted the content!")


    Create a New File:

    f = open("myfile.txt", "x") 

    Delete a File using os.remove() function: 

    import os 
    os.remove("demofile.txt")
    import os
    if os.path.exists("demofile.txt"):
      os.remove("demofile.txt")
    else:
      print("The file does not exists")

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!