Python File Handling – Writing, Appending, Deleting and File Methods
In this part, we’ll explore important functions on file handling and auxiliary methods.
Table of Contents
Writing into File
To write into file use w as the mode of access and call write()
function to the file object bypassing content to write.
file_res = open('m/test.txt', 'w') print(file_res.write("New \n Data \n\t Updated.")) file_res.close() #PYTHON OUTPUT 22 #Returns number of characters written to file.
Appending Content to File
We’ll be using the mode of access a to append additional data to the file.
file_res = open('m/test.txt', 'a') print(file_res.write("New data appended")) file_res.close() #PYTHON OUTPUT 18 #Returns number of characters written to file.
Deleting Files
To Unlink or delete a file from a directory there are a couple of different options explained below.
1) The os.remove()
function this removes a file in a given path. If the file does not exist then it raises FileNotFoundError
an exception.
If trying to remove the only directory than it raises IsADirectoryError
exception.
Syntax
os.remove(path, *, dir_fd=None)
Example
import os res = os.remove('m/test.txt') print(res)
2) The os.unlink()
function is similar to os.remove()
a function. The unlink is a traditional name in the UNIX system for the deletion of a file.
Syntax
os.unlink(path, *, dir_fd=None)
import os res = os.unlink('m/test.txt') print(res)
Other File Auxilary Methods
Here we’ll discuss some helpful file functions.
Extending the use of Mode of Access for a file.
The r+: Adding + addition operator after r we can read and write to the file at the same time.
path = 'm/test.txt' file_res = open(path, 'r+') print('File Object : {}'.format(file_res)) print('Is File Writable : {}'.format(file_res.readable())) print('Is File Readable : {}'.format(file_res.writable())) contents = file_res.read() print('File Contents : {}\n'.format(contents)) file_res.write("New data has added to this file.") new_contents = file_res.read() print('File Contents : {}'.format(new_contents)) #PYTHON OUTPUT File Object : <_io.TextIOWrapper name='m/test.txt' mode='r+' encoding='UTF-8'> Is File Writable : True Is File Readable : True File Contents : This is a text file New File Contents : This is a text file New data has added to this file.
Return File Encoding Type
This returns file encode type.
path = 'm/test.txt' file_res = open(path, 'r') print("The encoding of file is `{}` ".format(file_res.encoding)) #PYTHON OUTPUT The encoding of file is `UTF-8`
Return given File Path
path = 'm/test.txt' file_res = open(path, 'r') print("The given file path is `{}`".format(file_res.name)) #PYTHON OUTPUT The given file path is `m/test.txt`
Check if File is open or closed
The closed
attribute return a boolean value. If True
than the file is opened or else False
means the file has closed.
path = 'm/test.txt' file_res = open(path, 'r') print("Is File Closed : {}".format(file_res.closed)) #PYTHON OUTPUT Is File Closed : False
Truncate File Contents
Calling the truncate(pos) function on the file object removes all the content inside the file.
path = 'm/test.txt' file_res = open(path, 'r+') print("File Contents : {}\n".format(file_res.read())) file_res.truncate(0) file_res.seek(0) print("New File Contents : {}\n".format(file_res.read())) #PYTHON OUTPUT File Contents : This is Text File New File Contents :
size
to truncate()
function to skip some content and remove the rest of others.path = 'm/test.txt' file_res = open(path, 'r+') print("File Contents : {}\n".format(file_res.read())) file_res.truncate(10) file_res.seek(0) print("New File Contents : {}\n".format(file_res.read())) #PYTHON OUTPUT File Contents : This is Text File New File Contents : This is Te
Related Posts
- Working with Dictionary in Python
- Python Walrus Operator | Assignment Expression Operator with Examples




