import os
import subprocess
fileName = 'file.txt'
b = subprocess.check_output(['du','-sh', fileName]).split()[0].decode('utf-8')
print b
'' ' 如果b小于10MB然后继续别的打破 '''如何知道给定文件的大小是否小于10MB? Python的
import os
import subprocess
fileName = 'file.txt'
b = subprocess.check_output(['du','-sh', fileName]).split()[0].decode('utf-8')
print b
'' ' 如果b小于10MB然后继续别的打破 '''如何知道给定文件的大小是否小于10MB? Python的
您可以使用此
import os
os.path.getsize('path_to_dir/file.txt')
或
os.stat('path_to_dir/file.txt').st_size
同时,该是一个重复的问题。下次开始时请确保检查相同的问题是否已经存在。 干杯!
原来的答复:https://stackoverflow.com/a/2104107/5283213
使用os.stat
,并使用所得到的对象的st_size
构件:
import os
statinfo = os.stat('somefile.txt')
print statinfo
(33188, 422511L, 769L, 1, 1032, 100, 926L, 1105022698,1105022732, 1105022732)
print statinfo.st_size
926L
输出以字节为单位。
编辑检查
10MB
文件或不
很简单:使用if
声明和一些数学:
if statinfo.st_size <= 1048576: # and not 1000000 as 1024 * 1024
print "size is less than 10MB"
else:
print "greater than 10MB"
我想知道的是该文件的大小是否小于10MB。 这对获得正确答案将非常有帮助。 :) – Suneha
@SunehaBanuJ在那里,你应该这样做 – TheDarkKnight
嘿谢谢.. 它的工作:) – Suneha
选中此https://stackoverflow.com/questions/2104080/how python – arnold
为什么不先尝试自己[这里](https://stackoverflow.com/questions/6591931/getting-file-size-in-python) –