-3
import os
import subprocess

fileName = 'file.txt'

b = subprocess.check_output(['du','-sh', fileName]).split()[0].decode('utf-8')
print b

''' if b is less than 10MB then continue else break '''

Suneha Javid
  • 37
  • 1
  • 2
  • 12

2 Answers2

3

Original answer: https://stackoverflow.com/a/2104107/5283213

Use os.stat, and use the st_size member of the resulting object:

import os
statinfo = os.stat('somefile.txt')

print statinfo
(33188, 422511L, 769L, 1, 1032, 100, 926L, 1105022698,1105022732, 1105022732)

print statinfo.st_size
926L

Output is in bytes.

edit to check if 10MB file or not

It's simple: use an if statement and some maths:

if statinfo.st_size <= 10485760: # and not 10 000 000 as 1024 * 1024 * 10
    print "size is less than 10MB"
else:
    print "greater than 10MB"
shu
  • 28
  • 3
Sreetam Das
  • 3,226
  • 2
  • 22
  • 36
2

You could use this

import os
os.path.getsize('path_to_dir/file.txt')

or

os.stat('path_to_dir/file.txt').st_size 

Meanwhile, this is a duplicate question. Next time onwards do make sure to check if a question on the same already exists. Cheers!

crazyglasses
  • 530
  • 3
  • 10