how to know that the given file's size is less than 10MB or not? Python
本问题已经有最佳答案,请猛点这里访问。
1 2 3 4 5 6 7 | import os import subprocess fileName = 'file.txt' b = subprocess.check_output(['du','-sh', fileName]).split()[0].decode('utf-8') print b |
’’如果B小于10MB,则继续,否则中断’’
原始答案:https://stackoverflow.com/a/2104107/5283213
使用
1 2 3 4 5 6 7 8 | 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
很简单:使用
1 2 3 4 | if statinfo.st_size <= 1048576: # and not 1000000 as 1024 * 1024 print"size is less than 10MB" else: print"greater than 10MB" |
你可以用这个
1 2 | import os os.path.getsize('path_to_dir/file.txt') |
或
1 | os.stat('path_to_dir/file.txt').st_size |
同时,这是一个重复的问题。下一次,一定要检查是否已经存在相同的问题。干杯!