File handling is the important chapter to read in the programming language which helps to read, write files along with many other file handling options, to operate on files.
The concept of file handling has stretched over various other languages, but the implementation is either complicated or lengthy, but alike other concept of python this concept here is also easy and short.
- “ r “, for reading.
- “ w “, for writing.
- “ a “, for appending.
- “ r+ “, for both reading and writing
# a file named "filename", will be opened with the reading mode. file = open('filename.txt', 'r') # This will print every line one by one in the file for data in file: print (data)
Working with read() mode
we use file.read() to work on the read mode. read() is used to read the data from the file
# Python code to illustrate read() mode file = open("filename.text", "r") print file.read()
Creating a file using write() mode
Basically write() function is used to write the data in a txt file.
# Python code to create a file file = open('filename.txt','w') file.write("This is the write command") file.write("It allows us to write in a particular file") file.close()
Working with append() mode
append() is used to push the data in a file,
# Python code to illustrate append() mode file = open('filename.txt','a') file.write("This will add this line") file.close()
Login system using the file handling
import os.path if not os.path.exists('register.txt'): file = open('register.txt', 'w') file.close() # close garnu parxa file lai def register(): username = input('Enter username: ') if username in open('register.txt', 'r').read(): print('Username pahile thiyo ') exit() # if bata bahira aayeko password = input('Enter password: ') c_password = input('Enter confirm password: ') if password != c_password: print('Sorry kanchaa yespali') exit() handle = open('register.txt', 'a') handle.write(username) handle.write(' ') handle.write(password) # password write garyo handle.write('n') handle.close() print('Successfully register') exit() def login(): username = input('Enter username: ') password = input('Enter password: ') get_data = open('register.txt', 'r').readlines() users_data =  for user in get_data: users_data.append(user.split()) total_user = len(users_data) increment = 0 login_success = 0 while increment < total_user: usernames = users_data[increment] passwords = users_data[increment] if username == usernames and password == passwords: login_success = 1 increment += 1 if login_success == 1: print('Welcome ' + username) else: print('invalid username & password') register() login()
File handling with csv
import csv import os.path with open('student.csv') as student_list: data = csv.reader(student_list) for user in data: y= user z = user x= user if x is "a": file = open('student.txt', 'a') file.write(x+" ") file.write(y+ " ") file.write(z+" ") file.write('n') file.close() elif x is "r": file = open('students.txt', 'a') file.write(x+ " ") file.write(y+ " ") file.write(z+ " ") file.write('n') file.close()
student.csv contains the following data
name,age,phone achyut,22,9843450482 ram,24,98435371 anusha,22,985694731 akash,21,98545273 aliza,21,98611232