how to open files in python

File Handling in python

Introduction

In python file handling ,how to open  files in python  is the common questions in python file handling , File handling is very essential for data analytics means, if you are using CSV formatted file ,then these concept are very vital to understand.

Unlike the file handling in C or C++ , python handles the files very efficiently without importing any special library functions. 

The only difference is instead of calling FD or FP (File descriptor or File pointer) ,we use the term file object, which acts like file pointer .

For Eg:

File = open (‘/users/apple/hello.txt)

The above code opens the file in read mode, if you want to can specify or leave. But you should specify the path name properly , the path name may be absolute or relative.

 

 

File types

python supports 2 types of files, text and binary files.

Text files stores the data in the sequential manner , each line terminated with a special character known as EOL or a special character.

Whereas Binary file is entirely differ from text file ,it can be processed only through the applications. Because the file structure can be understood by application.

Open()

How to open a files in python? 

The answer to the above questions is here, 

The files are opened using open() method.

The syntax to open a file is as follows:
File_Object = open(“pathname” , ‘mode’)

File_Object is like FD or FP.

pathname is the location of the file

mode is for which purpose the file is opened like for reading or writing.

 

Mode

Including a mode value is optional in open () only for reading purpose ,if it is for writing , we need to include mode as second parameter.

the mode can take following values:

r – reading (Only for reading)

w- writing (Erasing the previous content or writing the new content)

a – append (To add the data at the end of the file).

 

File = open (‘test.txt’,’w’)

The above code open the file in write mode, make sure the path name is correct.

Crating a file

The above image illustrate the creation of a file in write mode.

Reading a file in python

There are different ways to read a file in python ,we discuss all one by one.

  1. using file.read() : In which we open the file use the method directly to read the content.

file = open(‘test.txt’,’r’)

print file.read()

2. In another method we can specify the number of character to be read .

print file.read(5)

This will read only 5 characters to the console.

3. In this type we use the readline() to read the content.

file = open(‘test.txt’ , ‘r’)

file.readline().

Here also we can pass a number to read specific line.

Writing the content to a file

The following code snippet will write the content to a file.

file = open(“test.txt”, “w”)

file.write(“This is a test series ”) 
file.write(“ adding more lines.)

file.close()

The above program ,open a file named test.txt in write mode , add the content using the write method ,then close the file object using the close().

 

Closing a file

we will close the file using the close().

The syntax is as follows:

file_object.close().

After completing all the file operation ,we need to call this method to close the file properly .

 

Using the with statement for file handling

To read a content from a file using with statement .

with open(‘hello.txt’ , ‘r’) as f 

content = f.read().

To write the content ,we use the following code snippet.

with open(‘hello.txt’ , ‘w’) as f 

f.write(‘Hello world).

 

Conclusion

File handling in python is very easy compared to other programming language like C , C++ or Java. Still many ways are there to handle the files in python ,I will update the content as soon as I got the new information , if any suggestion to improve article are most welcome. 

Scroll to Top