{"id":4991,"date":"2023-10-26T15:53:01","date_gmt":"2023-10-26T10:23:01","guid":{"rendered":"https:\/\/python-programs.com\/?p=4991"},"modified":"2023-11-10T12:00:38","modified_gmt":"2023-11-10T06:30:38","slug":"python-three-ways-to-check-if-a-file-is-empty","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-three-ways-to-check-if-a-file-is-empty\/","title":{"rendered":"Python: Three ways to check if a file is empty"},"content":{"rendered":"
This article is about to check if a file is empty in Python. We will use three different ways to check if a file is empty or not.<\/p>\n
The three ways are<\/p>\n
os.stat()<\/code> method.<\/li>\n- Using \u00a0
os.path.getsize()<\/code> method.<\/li>\n- Reading its first character.<\/li>\n<\/ol>\n
Method-1 : Use os.stat() to check if a file is empty :<\/h3>\n
In python, there is a stat()<\/code> function which can be used to get the statistics about a file.<\/p>\nos.stat(path, *, dir_fd=None, follow_symlinks=True)<\/pre>\nThis function<\/p>\n
\n- It accepts file path (string type) as an argument.<\/li>\n
- It returns an object of the structure stat containing various attributes. (Like
st.size<\/code> to know size of the file)<\/li>\n<\/ul>\n#Program :\r\n\r\n#importing the os module\r\nimport os\r\n\r\n#file path as string type\r\npath_of_file = 'sample.txt'\r\n\r\n# checking if size of file is 0\r\nif os.stat(path_of_file).st_size == 0:\r\n print('File is empty')\r\nelse:\r\n print('File is not empty')<\/pre>\nOutput :\r\nFile is empty<\/pre>\nAs our file is empty, so it returned ‘File is empty’.<\/p>\n
But we have to be more careful while using it, because if the file does not exist at the respective path then FileNotFoundError<\/code>.<\/p>\nFileNotFoundError: <\/span>[<\/span>WinError <\/span>2<\/span>]<\/span> The system cannot find the file specified: FILE_NAME<\/span><\/pre>\nSo by keeping that in mind first we will check the file exists or not. We will create a separate function for that. After checking if we will find the file exists then we will check the file is empty or not.<\/p>\n
So, let’s see the implementation of it.<\/p>\n
#Program :\r\n\r\nimport os\r\n\r\n#Checking the file exists or not\r\ndef check_file_empty(path_of_file):\r\n #Checking if file exist and it is empty\r\n return os.path.exists(path_of_file) and os.stat(path_of_file).st_size == 0\r\n\r\npath_of_file = 'sample.txt'\r\n\r\n# checking if file exist and it is empty\r\nEmptyOrNot = check_file_empty(path_of_file)\r\nif EmptyOrNot:\r\n print('File is empty')\r\nelse:\r\n print('File is not empty')<\/pre>\nOutput :\r\nFile is empty<\/pre>\nMethod-2 : Check if file is empty using os.path.getsize() in Python :<\/h3>\n
In Python, os module<\/code> there is an another function, by using which we can check if a file is empty or not.<\/p>\nos.path.<\/span>getsize<\/span>(<\/span>path<\/span>)<\/span><\/pre>\nThis function takes file path as an argument and returns the size in bytes. If the file does not exists it gives FileNotFoundError<\/code>.<\/p>\n#program :\r\n\r\nimport os\r\n\r\n#file path\r\npath_of_file = 'sample.txt'\r\n\r\n# checking if size of file is 0\r\nif os.path.getsize(path_of_file) == 0:\r\n print('File is empty')\r\nelse:\r\n print('File is not empty')<\/pre>\nOutput : \r\nFile is empty<\/pre>\nMethod-3 : Check if the file is empty by reading its first character in Python :<\/h3>\n
We can also check a file is empty or not by reading first character of the file. So, for that it opens the file in read only mode. Then it tries to read first character of the string. If it finds first character in the file then file exists and if it does not find the first character then the file is empty.<\/p>\n
#Program :\r\n\r\ndef file_empty(file_name):\r\n # open file in read mode\r\n # Reading first character to check file is empty or not\r\n with open(file_name, 'r') as read_obj:\r\n # reading the first character\r\n one_char = read_obj.read(1)\r\n # if first character not fpound then file is empty\r\n if not one_char:\r\n return True\r\n return False\r\n\r\npath_of_file = 'mysample.txt'\r\n# check if file is empty\r\nEmptyOrNot = file_empty(path_of_file)\r\nprint(EmptyOrNot)<\/pre>\nOutput : \r\nFile is empty<\/pre>\n","protected":false},"excerpt":{"rendered":"Three ways to check if a file is empty in Python. This article is about to check if a file is empty in Python. We will use three different ways to check if a file is empty or not. The three ways are Using os.stat() method. Using \u00a0os.path.getsize() method. Reading its first character. Method-1 : …<\/p>\n