File Handling in Python: Create, Open, Append, Read, Write Operations

2457
File Handling in Python
File Handling in Python

File Handling in Python: Create, Open, Append, Read, Write Operations

File Handling in Python is a very basic concept, although some peoples might know File Handling operations like to create, read and write files, and other File Handling in Python.

Python is an interpreted, high-level, general-purpose programming language. Created by Guido van Rossum and first released in 1991, Python’s design philosophy emphasizes code readability with its notable use of significant whitespace

File handling is an important part of any web application. Python has several functions for creating, reading, updating, and deleting files.

File Handling: To Create a Text File

file= open("cybertecz.txt","w+")
for i in range(10):
     file.write("This is line %d\r\n" % (i+1))
	file.close() 

File Handling: To Create a file using write()

file = open('cybertecz.txt','w') 
file.write("This is the write command") 
file.write("It allows us to write in a particular file") 
file.close() 

File Handling: To use open() function

file = open('cybertecz.txt', 'r') 
for each in file: 
    print (each) 

File Handling: To Append Data

file = open('cybertecz.txt','a') 
file.write("This will add this line") 
file.close() 
See the source image

File Handling: To Read a File

file=open("cybertecz.txt", "r")
	if file.mode == 'r':
	contents =file.read()

File Handling: To Read a File line by line

File Handling in Python allows you to read your .txt file line by line if your data is too big to read.

file=open("cybertecz.txt", "r")
    if file.mode == 'r':
	contents =file.read()
f1=file.readlines()
for x in f1:
    print(x)

File Handling: To delete a File

import os
os.remove("demofile.txt")

Check it out Latest Jobs for Python Developer: Click here

If You Want To Get More Daily Such Jobs Updates, Career Advice Then Join the Telegram Group From Given Link And Never Miss Update.

Join Telegram Group of Daily Jobs Updates for 2010-2021 Batch: Click Here

Why You’re Not Getting Response From Recruiter?: Click here

How To Get a Job Easily: Professional Advice For Job Seekers: Click here

Cognizant Latest News: Up To 20K+ Employees Will Be Hired: Click here

COVID-19 Live Tracker India & Coronavirus Live Update: Click here

Why Remove China Apps took down from Play store?: Click here

Feel Like Demotivated? Check Out our Motivation For You: Click here

List of Best Sites To Watch Free Movies Online in 2020: Click here

5 Proven Tips For How To Look Beautiful and Attractive: Click here

Feel Like Demotivated? Check Out our Motivation For You: Click here