To check if a file is empty in R, you can use either the “file.info()’s size attribute” or the “file.size()” function.
We must check if a file is empty to save our resources from reading or processing. It helps us put a data validation that will prevent unexpected results or errors. Based on its emptiness, we can make an informed decision.
In my current working directory, I have two files:
As the name suggests, empty.txt is an empty file, and data.txt contains some data.
The file.info() is a built-in R function that retrieves information about the file, including size, permission, modification, time, and other metadata.
The file.info() function returns a file object containing various information, and one is the “size” attribute. The $size attribute suggests the file size in bytes. If $size is 0, the file is empty.
Let’s check for an empty.txt file:
# Getting file object
file_info <- file.info("empty.txt")
# Checking if the file is empty by comparing its size to 0
if (file_info$size == 0) {
cat("The file is empty.\n")
} else {
cat("The file is not empty.\n")
}
Output
The file is empty.
As expected, since we knew that the file was empty, it gave the correct result.
Let’s check for data.txt file:
# Getting file object
file_info <- file.info("output/data.txt")
# Checking if the file is empty by comparing its size to 0
if (file_info$size == 0) {
cat("The file is empty.\n")
} else {
cat("The file is not empty.\n")
}
Output
The file is not empty.
If you want to check file size as well as other metadata of the file, I highly recommend you use this approach. However, it might be less concise if you only care about checking emptiness because, for that, there is an easy way.
The file.size() is the simplest way because it returns the file size in bytes directly. If the size is 0, it means the file is empty; otherwise, it is not an empty file.
Let’s check for an empty.txt file:
file_path <- "empty.txt"
# Checking if the file is empty by comparing its size to 0
if (file.size(file_path) == 0) {
cat("The file is empty.\n")
} else {
cat("The file is not empty.\n")
}
Output
The file is empty.
Let’s check for a data.txt file:
file_path <- "output/data.txt"
# Checking if the file is empty by comparing its size to 0
if (file.size(file_path) == 0) {
cat("The file is empty.\n")
} else {
cat("The file is not empty.\n")
}
Output
The file is not empty.
If your goal is solely to find whether the file is empty or not, use the .size() approach. This approach does not give more information about the file’s metadata.
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.
Before executing an operation on an object, it is advisable to check its length, as…
Rounding is a process of approximating a number to a shorter, simpler, and more interpretable…
Whether you want to add new data to your existing datasets or create new variables…
The square root of a number is a value that is multiplied by itself, giving…
Duplicate rows refer to all the values across all columns that are the same in…
A vector is a data structure that holds the same type of data. When working…