I am doing backups in python script but i need to get the size of tar.gz file created in MB
How can i get the size in MB of that file
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s not clear from your question whether you want to the compressed or uncompressed size of the file, but in the former case, it’s easy with the
os.path.getsizefunction from the os moduleTo get the answer in megabytes you can shift the answer right by 20, e.g.
Although that operation will be done in integers – if you want to preserve fractions of a megabyte, divide by
(1024*1024.0)instead. (Note the.0so that the divisor will be a float.)Update: In the comments below, Johnsyweb points out a useful recipe for more generally producing human readable representations of file sizes.