Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Python is a high-tech programming language with built-in functions and hence easy to learn for beginners. In this article, we will be discussing about one of the built-in functions, i.e., the Python truncate function. We will be starting with the introduction of the truncate function, followed by the prerequisites and its implementation.
So, let's start with the topic with an introduction to Python truncate function.
The word 'TRUNCATE' means to shorten something by a given size. The same job is done by the truncate function; it is used to shorten the length of a given file. Size is considered an optional parameter and referred to as the number of bytes upto which the file needs to be truncated. The syntax of the truncate function is as follows:
If the size is specified, then the file will be truncated almost to that size; otherwise, the size is set to the file's current position. If the size given is greater than the current size, the result depends on the platform; the file's size may remain unchanged or increase to the given size.
As there is no return value of the Python truncate function, you can check the file size in the file properties. You need to right-click on the given file and click on the properties option as shown in the below image.
The truncate function works on files; hence, it is a prerequisite to be familiar with file objects. The file objects allow us to access and manipulate files using various methods and attributes. Using these objects, we can read and write any number of files. Let's discuss some of the primary file methods required during truncation in Python.
Open
The open function in Python is used to open stored files. It returns a file object containing the file content. The syntax for this function is as follows:
The argument 'mode' is used to specify the mode of the file to be opened. The open function has six different modes. These are:
Modes
Use
"r"
To open the file in read-only mode. If no other mode is specified, it is passed as the default mode. It returns an error if the file does not exist.
"w"
To open the file in writing mode. If the specified file does not exist, this mode creates a new file for you.
"a"
To open the file for adding content to an already existing file. If the specified file does not exist, this mode creates a new file for you.
"x"
This mode is used to create a new file with a specified name.
"b"
This mode handles image files and opens the file in binary mode.
"t"
By default, text mode is used by the open function. This mode is used to open the specified file in text mode.
Python provides three built-in methods for reading the data of a file. These are:
Read (n) = It is used to read the entire file. If the value of 'N' is specified, it reads only 'N' bytes from the file.
Readline (n) = It is used to read only one line. Only 'N' bytes will be read if ' N' is given, but not more than a line.
Readlines() = It is used to read all the lines and return each as a list of strings.
Write
Similar to the read function, Python provides built-in methods for writing over the file. There are two ways to write on a file. These are:
Write (string) = This function inserts the string as a single line in the specified file.
Writelines(Lines) where Lines = [string1, string2, string3, ....] = It is used to insert a list of multiple strings at the same time in the given file.
Code
Let's see an example of open, read and write functions:
Example
# Python program to write in a file
# Open a file in read mode
fileObject = open('file.txt', 'r')
print("Before writing file contain : ")
print(fileObject.read())
fileObject.close()
# Open the file in write mode
fileObject = open('file.txt', 'w')
#Define lines and string to be written
str = "Coding Ninjas\n"
lines = ["This is text1 \n", "This is text2 \n", "This is text3 \n"]
# Working of write(string) function
fileObject.write(str)
# Working of writelines(lines) function
fileObject.writelines(lines)
fileObject.close()
# Check the file after writing
fileObject = open('file.txt','r')
print("\nAfter writing the file contain : ")
print(fileObject.read())
fileObject.close()
You can also try this code with Online Python Compiler
We have created a new file inside a new folder on the desktop. The file contains the text “This is a new file”.
We opened the file in read mode and read the file’s content. Now close the file and open it again in write mode.
We created a string and a list of strings. Write this string and a list of strings to the file using the write function. As the write function overwrites the existing content. So now the file contains only a string and a list of strings.
Now close the file and open it in read mode. Read the file content after writing and print the result.
We are now familiar with the primary functions required in the Python truncate function. Let's now see an example for clear understanding.
Create a new file, 'file.txt' in a new folder on your desktop.
Right-click on the file.
Click the Properties option to see the current file size.
The result will be as follows:
Initially, the file contains
As the size of our file is 100 bytes. Let's truncate it to 50 Bytes. To do so, run the following code:
# Program to use truncate function
# Open a file
fileObject = open('file.txt', 'w')
# Truncate the file
fileObject.truncate(50)
# Closing the file
fileObject.close()
You can also try this code with Online Python Compiler
The Python truncate function is used to resize the given file to the given number of bytes.
What are the functions for reading a file in Python?
Python provides three built-in functions for reading the file data. These are read(), readline(), and readlines().
Does the Python truncate function have a return value?
The truncate() function does not have a return type. We can check the file size from the file properties option to check the result.
What if the size given is greater than the file's current size?
If the size specified is greater than the file's current size, then the result is platform-dependent. The file size may remain unchanged or increase to the given size.
What are the different file opening modes in Python?
The open function in Python enables us to use six different file-opening modes. These are read(r), write(w), append(a), binary(b), text(t) and create(x).
Conclusion
We have briefly discussed the Truncation in Python and different file objects in Python. We also discussed the different file objects, an example of the Python truncate function, and its explanation. We hope that we have helped you achieve a better grip on the topic.
If you found this one interesting, explore our other related blogs as well: