我如何获得python中变量的字节数,就像unix中的wc -c一样

ran*_*psp 1 python tar

我面临着大量数据文件的问题.我需要跳过对这些文件执行某些操作.我将文件的数据转换为变量.现在我需要获取变量的字节,如果它大于102400,则打印一条消息.

更新:我无法打开文件,因为它存在于tar文件中.内容已经被复制到一个名为'data'的变量中,我可以打印变量数据的内容.我只需要检查它是否超过102400字节.

谢谢

Rob*_*tie 6

import os
length_in_bytes = os.stat('file.txt').st_size
if length_in_bytes > 102400:
   print 'Its a big file!'
Run Code Online (Sandbox Code Playgroud)

更新以处理tarfile中的文件

import tarfile
tf = tarfile.TarFile('foo.tar')
for member in tarfile.getmembers():
    if member.size > 102400:
        print 'It's a big file in a tarfile - the file is called %s!' % member.name
Run Code Online (Sandbox Code Playgroud)