{"id":5983,"date":"2021-08-26T17:35:33","date_gmt":"2021-08-26T12:05:33","guid":{"rendered":"https:\/\/python-programs.com\/?p=5983"},"modified":"2021-11-22T18:39:30","modified_gmt":"2021-11-22T13:09:30","slug":"python-get-file-size-in-kb-mb-or-gb-human-readable-format","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-get-file-size-in-kb-mb-or-gb-human-readable-format\/","title":{"rendered":"Python: Get file size in KB, MB, or GB \u2013 human-readable format"},"content":{"rendered":"
In this tutorial of getting the file size in KB, MB, or GB in Human-readable format in Python, we are going to see how we can get the sizes of different files in bytes, kilobytes, megabytes, gigabytes, etc.<\/p>\n
In this function, we pass the path of the file into it and if the file exists it returns the file size in bytes. If the file does not exist it throws Syntax:<\/em><\/p>\n <\/p>\n Source Code:<\/em><\/p>\n We can also find the size of the file using a stats( ) <\/em><\/strong>function available in Python\u2019s OS module. It takes the file path as an argument and returns an object containing the file details.<\/p>\n Syntax:<\/em><\/p>\n <\/p>\n Source Code: <\/em><\/p>\n <\/p>\n We can get the file sizes using the above method but we can not convert them to KB, MB, GB, TB directly so we have to create a function. The function would convert the units and so us the size of the file in our desired unit.<\/p>\n Here is a conversion table to switch from bytes to another unit of measurement :<\/p>\n et file size in human-readable units like kilobytes (KB), Megabytes (MB), or GigaBytes (GB<\/p>\n In case the file does not exist it will throw an error disrupting our program\u2019s execution. So to handle that we can check if the file exists or not and if not then handle it by using the if-else <\/em>block.<\/p>\n In this tutorial of getting the file size in KB, MB, or GB in Human-readable format in Python, we are going to see how we can get the sizes of different files in bytes, kilobytes, megabytes, gigabytes, etc. Different ways to get file size in Bytes, Kilobytes, Megabytes, Gigabytes, etc Get file size using os.path.getsize() …<\/p>\nos.error<\/code>.<\/p>\n
Syntax : os.path.getsize(path)<\/pre>\n
# Program to Get\u00a0file\u00a0size\u00a0in\u00a0bytes\u00a0using\u00a0os.path.getsize()\r\n\r\nimport os\r\ndef fileSizeInBytes(filePath):\r\n \"\"\"Size of the file in bytes\"\"\"\r\n sizeOfFile = os.path.getsize(filePath)\r\n return sizeOfFile\r\nfilePath = 'movie.mkv'\r\n#Getting the size of the file from by passing the path into the function\r\nsizeOfFile = fileSizeInBytes(filePath)\r\nprint('Size of the file(In Bytes) - ', sizeOfFile)\r\n<\/pre>\n
Output :\r\nSize of the file(In Bytes) -\u00a0 349016027<\/pre>\n
Method-2: Get file size in bytes using os.stat().st_size<\/h4>\n
os.stat(path, *, dir_fd=None, follow_symlinks=True)<\/pre>\n
# Program to Get\u00a0file\u00a0size\u00a0in\u00a0bytes\u00a0using\u00a0os.stat().st_size\r\n\r\nimport os\r\ndef fileSizeInBytes(filePath):\r\n \"\"\"Find the size of the file in bytes\"\"\"\r\n statOfFile = os.stat(filePath)\r\n #Size of file from the stats\r\n sizeOfFile = statOfFile.st_size\r\n return sizeOfFile\r\nfilePath = 'movie.mkv'\r\n#Getting the size of the file from by passing the path into the function\r\nsizeOfFile = fileSizeInBytes(filePath)\r\nprint('Size of the file(In Bytes) - ', sizeOfFile)\r\n<\/pre>\n
Output :\r\nSize of the file(In Bytes) -\u00a0 349016027<\/pre>\n
Method-3: Get file size in bytes using pathlib.Path.stat().st_size<\/h4>\n
# program to Get\u00a0file\u00a0size\u00a0in\u00a0bytes\u00a0using\u00a0pathlib.Path.stat().st_size\r\n\r\nfrom pathlib import Path\r\ndef fileSizeInBytes(filePath):\r\n \"\"\"Size of the file in bytes\"\"\"\r\n fileObj = Path(filePath)\r\n #Size of file from the stats\r\n sizeOfFile = fileObj.stat().st_size\r\n return sizeOfFile\r\nfilePath = 'movie.mkv'\r\n#Getting the size of the file from by passing the path into the function\r\nsizeOfFile = fileSizeInBytes(filePath)\r\nprint('Size of the file(In Bytes) - ', sizeOfFile)\r\n<\/pre>\n
Output :\r\nSize of the file(In Bytes) -\u00a0 349016027<\/pre>\n
Method-4: Get file size in human-readable units like kilobytes (KB), Megabytes (MB), or GigaBytes (GB)<\/h4>\n
\n\n
\n Units<\/strong><\/td>\n Number of bytes<\/strong><\/td>\n<\/tr>\n \n KiloBytes<\/strong><\/td>\n 1024<\/td>\n<\/tr>\n \n MegaBytes<\/strong><\/td>\n 1024*1024 = 1\u00a0048\u00a0576\u202c<\/td>\n<\/tr>\n \n GigaBytes<\/strong><\/td>\n 1024*1024*1024 = 1\u00a0073\u00a0741\u00a0824<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n #Program to Get file size in human-readable units like KB, MB, GB, TB\r\n\r\nimport enum\r\nimport os\r\n\r\nclass sizeUnit(enum.Enum):\r\n #class to store the various units\r\n BYTES = 1\r\n KB = 2\r\n MB = 3\r\n GB = 4\r\n\r\ndef unitConvertor(sizeInBytes, unit):\r\n #Cinverts the file unit\r\n if unit == sizeUnit.KB:\r\n return sizeInBytes\/1024\r\n elif unit == sizeUnit.MB:\r\n return sizeInBytes\/(1024*1024)\r\n elif unit == sizeUnit.GB:\r\n return sizeInBytes\/(1024*1024*1024)\r\n else:\r\n return sizeInBytes\r\n\r\ndef fileSize(filePath, size_type):\r\n \"\"\"File size in KB, MB and GB\"\"\"\r\n size = os.path.getsize(filePath)\r\n return unitConvertor(size, size_type)\r\n\r\nfilePath = 'movie.mkv'\r\nsizeOfFile = fileSize(filePath,sizeUnit.BYTES)\r\nprint('Size of the file in - ', sizeOfFile)\r\n#Printing the size of file in KB\r\nsizeOfFile = fileSize(filePath,sizeUnit.KB)\r\nprint('Size of the file in KB - ', sizeOfFile)\r\n#Printing the size of file in MB\r\nsizeOfFile = fileSize(filePath,sizeUnit.MB)\r\nprint('Size of the file in MB - ', sizeOfFile)\r\n#Printing the size of file in GB\r\nsizeOfFile = fileSize(filePath,sizeUnit.GB)\r\nprint('Size of the file in GB - ', sizeOfFile)\r\n<\/pre>\n
Output :\r\nSize of the file in -\u00a0 349016027\r\nSize of the file in KB -\u00a0 340835.9638671875\r\nSize of the file in MB -\u00a0 332.8476209640503\r\nSize of the file in GB -\u00a0 0.32504650484770536<\/pre>\n
Check if the file exists before checking for the size of the file<\/h3>\n
# Program to check\u00a0if\u00a0the\u00a0file\u00a0exists\u00a0before\u00a0checking\u00a0for\u00a0the\u00a0size\u00a0of\u00a0the\u00a0file\r\n\r\nimport os \r\n\r\ndef fileSizeInBytes(filePath):\r\n \"\"\" Size of the file in bytes\"\"\"\r\n sizeOfFile = os.path.getsize(filePath)\r\n return sizeOfFile\r\n\r\n#Passing a file name that does not exist into the program\r\nfileName = 'nonExistentFile.txt'\r\nif os.path.exists(fileName):\r\n size = fileSizeInBytes(fileName)\r\n print('Size of file in Bytes : ', size)\r\nelse:\r\n print('File not there')\r\n<\/pre>\n
Output :\r\nFile not there<\/pre>\n","protected":false},"excerpt":{"rendered":"