Python File Open | How to handel file in python

File Handling

python file


File handling is an important part of any web application. Python provides the facility of working on Files. A File is an external storage on a hard disk from where data can be stored and retrieved. Python has several functions for creating, reading, updating, and deleting files.


Opening a Files

Before working with Files you have to open the File. To open a File, Python built-in function open() is used. It returns an object of File which is used with other functions. Having opened the file now you can perform read, write, etc. operations on the File.

syntax

fileobj=open(filename , mode , buffer)


Filename

The file_name argument is a string value that contains the name of the file that you want to access.


Mode


The mode determines the mode in which the file has to be opened, i.e., read, write, append, etc. This is an optional parameter and the default file access mode is read (r).


Buffer

if the value is set to zero (0), no buffering will occur while accessing a file, if the value is set to top one (1), line buffering will be performed while accessing a file.

List of the different modes of opening a file

Advertisement

Mode Description
r Opens a file for reading only. (It's a default mode.)
w Opens a file for writing. (If a file doesn't exist already, then it creates a new file. Otherwise, it's truncate a file.)
x Opens a file for exclusive creation. (Operation fails if a file does not exist in the location.)
a Opens a file for appending at the end of the file without truncating it. (Creates a new file if it does not exist in the location.)
t Opens a file in text mode. (It's a default mode.)
b Opens a file in binary mode.
+ Opens a file for updating (reading and writing.)

Example:

file=open("abc.txt","w")
print ("Name of the file: ", file.name )
print ("Closed or not : ", file.closed )
print ("Opening mode : ", file.mode)

Output:

Name of the file: abc.txt
Closed or not : False
Opening mode : w

Advertisement


Closing a Files

Once you are finished with the operations on File at the end you need to close the file. It is done by the close() method. the close() method is used to close a File.

Syntax

fileobj.close()


Writing to a File

write() method is used to write a string into a file.

Syntax

fileobj.write(string,str)


Reading from a File

read() method is used to read data from the File. value is the number of bytes to be read. In case, no value is given it reads till the end of the file is reached.

Syntax

fileobj.read(value)

Example:

file=open("abc.txt","w")
file.write("Hello Python")
file.close()
file1=open("abc.txt","r")
a=file1.read()
print(a)
file1.close()
file1=open("abc.txt","r")
a=file1.read(5)
print(a)
file1.close()

Output:

Hello Python
Hello


rename() Method

The rename() method takes two arguments, the current filename, and the new filename.

Example:

import os
os.rename("abc.txt","xyz.txt")


remove() Method

You can use the remove() method to delete files by supplying the name of the file to be deleted as the argument.

Example:

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

Advertisement

Next Post Previous Post
No Comment
Add Comment
comment url