File handling is an important part of any web application. Python has several functions for creating, reading, updating, and deleting files.
Types Of File in Python
The key function for working with files in Python is the open() function. This function takes two parameters; filename, and mode.
Syntax :
file_object = open ( file_name , mode )
Once the file is opened, you can perform various operations on it, such as reading or writing to it. It is important to close the file once you are done with it, using the close() method.
The code is how to open a file in Python using the open() function. The open() function takes two parameters: the first is the name of the file, and the second is the mode in which the file should be opened. The mode can be 'r' for reading, 'w' for writing, and 'a' for appending.
In this example, the file "ram.txt" is opened in write mode using 'w'
try: f=open("ram.txt",'w') #f=open("data.txt",'a') #f=open("data.txt",'r') #print(f.read()) #print(f.readline()) """ for line in f: print(line) """ f.write("\nThis is New Line") except FileNotFoundError: print("File not Found") else: print("Thank You") f.close()To download raw file Click Here
This is a New Line Thank You
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions