Working with Python -- File Handling

Introduction

File Handling is a very important portion for any application. In Python, we have several function to Create, Read, Update and Delete file. In this blog post, we are trying to discuss about Python File handling.


Hope it will be interesting.

Open Function

This is the key function to working with file. The Open function takes two parameters. One is File name and second is mode.

"r" Read - Default value. Opens a file for reading, error if the file does not exist

"a" - Append - Opens a file for appending, creates the file if it does not exist

"w" - Write - Opens a file for writing, creates the file if it does not exist

"x" - Create - Creates the specified file, returns an error if the file exists

In addition of that, we can specify that the file can be treated as Binary or as Text file.

"t" - Text - Default value. Text mode

"b" - Binary - Binary mode (e.g. images)

Example:

f = open("demofile.txt")
f = 
open("demofile.txt""rt")

Here in second like it is going to open a file named demofile.txt with read and text mode.

 

Complete example to open a file from different location.

f = open("D:\\demo\welcome.txt""r")
print(f.read())

 

Reading the parts of a File

Suppose we have a file named welcome.txt

Hello Mayuree,
Welcome to India.

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

Output:

Hello

It reads inly first five character from file.

Read the Entire line

To read an entire line form the file, we use ResadLine() method.

Example:

f = open("demofile.txt""r")
print(f.readline())

Output:

Hello Mayuree,

f = open("demofile.txt""r")
print(f.readline())
print(f.readline())

Output:

Hello Mayuree,
Welcome to India.


Close the File

The good practice is that; always close the file after use.

f = open("demofile.txt""r")
print(f.readline())
f.close()

Output:

Hello Mayuree,


Writing to an Existing File or Overwrite it

We can use “a” or “w” with Open() function. “a” is for appending in an existing file or “w” is for just overwrite the existing file.

Example:

f = open("demofile.txt""a")
f.write(
"How are you.")
f.close()

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

Output:

Hello Mayuree,
Welcome to India.
How are you.

Example:

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

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

Output:

I have deleted the content!

 

Create a new file

To create a new file we are using “x” mode with Open() function.

Example:

f = open("myNewfile.txt""w")

 

Remove a File or Folder

To remove a file or folder we need to import OS module.

Example:

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


It search the demofile.txt and if found delete it.

To delete folder.

Example:

import os
os.rmdir(
"myfolder")

 

Hope you like it.

Comments

Popular Posts

Incremental Load by using ADF

Copying Multiple File from Blob Storage to Single SQL Table – Part-2