Blog Details

img
Data Science

what is python files Io

Spoke Right / 14 Nov, 2023

File handling simply means to open a file and to process it according to the required tasks. Python facilitates several functions to create, read, write, append, delete and close files.

❏     Open File:

To open a file in Python, open() function is used.

Syntax:

f = open(“filename“, “mode“)

Filename:  This parameter specifies the name of the file to be opened.

Mode: This parameter specifies the mode in which the file should be opened.

Different MODES of open function:

$modeMODEDESCRIPTION
rRead only modePointer starts from the beginning of the file.
wWrite only modeOverwrites the existing file or creates a new file if it doesn’t exist. Pointer starts from the beginning of the file.
aWrite only modeContinues writing in the existing file or creates a new file if it doesn’t exist. Pointer starts from the end of the file.
r+Read Write modePointer starts from the beginning of the file.
w+Read Write modeOverwrites the existing file or creates a new file if it doesn’t exist. Pointer starts from the beginning of the file.
a+Read Write modeContinues writing in the existing file or creates a new file if it doesn’t exist. Pointer starts from the end of the file.
rbRead only mode in Binary format.Pointer starts from the beginning of the file.
wbWrite only mode in Binary format.Overwrites the existing file or creates a new file if it doesn’t exist. Pointer starts from the beginning of the file.
abWrite only mode in Binary format.Continues writing in the existing file or creates a new file if it doesn’t exist. Pointer starts from the end of the file.
rb+Read Write mode in Binary format.Pointer starts from the beginning of the file.
wb+Read Write mode in Binary format.Overwrites the existing file or creates a new file if it doesn’t exist. Pointer starts from the beginning of the file.
ab+Read Write mode in Binary format.Continues writing in the existing file or creates a new file if it doesn’t exist. Pointer starts from the end of the file.

❏     Close File:

To close a file in Python, close() function is used.

Syntax:

f.close()

❏     Read File:

●      To read a file in Python, read() function is used.

Syntax:

f.read()

To read a line of a file in Python, readline() function is used.

Syntax:

f.readline()

❏     Write File:

To write a file in Python, write() function is used.

Syntax:

f.write(statement)

❏     Delete File:

●      To delete a file in Python, os is imported and then os.remove() function is used.

Syntax:

import os

os.remove(“filename“)

To delete a folder in Python, os is imported and then os.rmdir() function is used.

Syntax:

import os

os.rmdir(“filename“)

Python File Handling Methods:

METHODSSYNTAXUSES
rename()os.rename(“existing_file_name”, “new_file_name”)To replace the existing python file name with a new python file name.
remove()os.remove(“file_name”)To delete a python file.
mkdir()os.mkdir(“file_name“)To create a directory to store python files.
chdir()os.chdir(“file_name“)To change the current working directory.
rmdir()os.rmdir(“directory_name”)To delete a directory.
tell()To get the exact position in the python file.
getcwd()os.getcwd()To get the current working directory.

Example:

f = open("pythonex.txt", "w")
f.write("HELLO PYTHON!\nPython facilitates several functions to create, read, write, append, delete and close files.")
f.close()
 
f = open("pythonex.txt", "r")
b = f.read()
print b
f.close()

0 comments

Warning: PHP Startup: Unable to load dynamic library 'imagick.so' (tried: /usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so (/usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so: cannot open shared object file: No such file or directory), /usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so.so (/usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0