{"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":"

Three ways to check if a file is empty in Python.<\/h2>\n

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

    \n
  1. Using os.stat()<\/code> method.<\/li>\n
  2. Using \u00a0os.path.getsize()<\/code> method.<\/li>\n
  3. 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>\n

    os.stat(path, *, dir_fd=None, follow_symlinks=True)<\/pre>\n

    This function<\/p>\n