Posted By

iloveitaly on 05/31/10


Tagged

file path python size mb megabyte


Versions (?)

Get File Size Python


 / Published in: Python
 

  1. def file_size_mb(filePath): return float(os.path.getsize(filePath)) / (1024 * 1024)

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: robbietree on June 13, 2011

If file does not exist, will raise an exception, wouldn't be more friendly to check if file exists first. Here is my code based on yours:

def filesizemb(filepath): if os.path.exists(filepath): return (float(os.path.getsize(filepath)))/1024 * 1024 return -1

Posted By: iloveitaly on July 15, 2011

Great addition. This snippit was being used within the context of code that was already checking for file existence, so I didn't think to add it to the function itself.

Thanks.

You need to login to post a comment.