Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 文件中还有多少未读字节?_Python_File - Fatal编程技术网

Python 文件中还有多少未读字节?

Python 文件中还有多少未读字节?,python,file,Python,File,我定期从文件中读取16位帧, 最后一帧我需要知道是否有足够的数据和文件对我的格式有效 f.read(16) 如果没有更多数据或至少有1个字节,则返回空字符串。 如何检查文件中还有多少未读字节?使用seek(0,2)和tell() BUFF=16 f=打开(“某个文件”、“r”) x=0 #移到文件末尾 f、 寻找(0,2) #获取当前位置 eof=f.tell() #返回到文件的开头 f、 寻道(0,0) #任意循环 当x

我定期从文件中读取16位帧, 最后一帧我需要知道是否有足够的数据和文件对我的格式有效

f.read(16)
如果没有更多数据或至少有1个字节,则返回空字符串。 如何检查文件中还有多少未读字节?

使用
seek(0,2)
tell()

BUFF=16
f=打开(“某个文件”、“r”)
x=0
#移到文件末尾
f、 寻找(0,2)
#获取当前位置
eof=f.tell()
#返回到文件的开头
f、 寻道(0,0)
#任意循环
当x<128时:
数据=f.read(BUFF)
x+=len(数据)
#打印剩余的未读字节数
未读=eof-x
未读打印
:

  • seek(offset[,whence])
    设置文件的当前位置,如stdio的fseek()。whence参数是可选的,默认为0 (绝对文件定位);其他值为1(相对于 当前位置)和2(相对于文件结尾进行搜索)。没有 返回值。请注意,如果打开文件进行附加(模式“a” 或“a+”),任何seek()操作都将在下一次写入时撤消。如果 该文件仅在附加模式(模式“a”)下打开以进行写入,如下所示: 方法本质上是一个no-op,但它对于打开的文件仍然有用 在启用读取的附加模式下(模式“a+”)。如果文件已打开 在文本模式下(不带“b”),只有tell()返回的偏移量是合法的。 使用其他偏移会导致未定义的行为。请注意,并非所有文件 对象是可查找的

  • tell()
    返回文件的当前位置,如stdio的ftell()


为此,您必须知道文件的大小。使用该对象,可以执行以下操作:

f.seek(0, 2)
file_size = f.tell()

变量
file\u size
将包含文件的大小(以字节为单位)。读取时,只需执行
f.tell()-file\u size
即可获得剩余字节数。所以:

也许更容易使用

def LengthOfFile(f):
    """ Get the length of the file for a regular file (not a device file)"""
    currentPos=f.tell()
    f.seek(0, 2)          # move to end of file
    length = f.tell()     # get current position
    f.seek(currentPos, 0) # go back to where we started
    return length

def BytesRemaining(f,f_len):
    """ Get number of bytes left to read, where f_len is the length of the file (probably from f_len=LengthOfFile(f) )"""
    currentPos=f.tell()
    return f_len-currentPos

def BytesRemainingAndSize(f):
    """ Get number of bytes left to read for a regular file (not a device file), returns a tuple of the bytes remaining and the total length of the file
        If your code is going to be doing this alot then use LengthOfFile and  BytesRemaining instead of this function
    """
    currentPos=f.tell()
    l=LengthOfFile(f)
    return l-currentPos,l


if __name__ == "__main__":
   f=open("aFile.data",'r')
   f_len=LengthOfFile(f)
   print "f_len=",f_len
   print "BytesRemaining=",BytesRemaining(f,f_len),"=",BytesRemainingAndSize(f)
   f.read(1000)
   print "BytesRemaining=",BytesRemaining(f,f_len),"=",BytesRemainingAndSize(f)

为什么不简单地检查
f.read(16)
返回的字符串的长度呢?如果它是16,好的,使用它,如果它小于16,就没有足够的数据了。在读取之前先获取文件大小如何
os.path.getsize(文件名)
def LengthOfFile(f):
    """ Get the length of the file for a regular file (not a device file)"""
    currentPos=f.tell()
    f.seek(0, 2)          # move to end of file
    length = f.tell()     # get current position
    f.seek(currentPos, 0) # go back to where we started
    return length

def BytesRemaining(f,f_len):
    """ Get number of bytes left to read, where f_len is the length of the file (probably from f_len=LengthOfFile(f) )"""
    currentPos=f.tell()
    return f_len-currentPos

def BytesRemainingAndSize(f):
    """ Get number of bytes left to read for a regular file (not a device file), returns a tuple of the bytes remaining and the total length of the file
        If your code is going to be doing this alot then use LengthOfFile and  BytesRemaining instead of this function
    """
    currentPos=f.tell()
    l=LengthOfFile(f)
    return l-currentPos,l


if __name__ == "__main__":
   f=open("aFile.data",'r')
   f_len=LengthOfFile(f)
   print "f_len=",f_len
   print "BytesRemaining=",BytesRemaining(f,f_len),"=",BytesRemainingAndSize(f)
   f.read(1000)
   print "BytesRemaining=",BytesRemaining(f,f_len),"=",BytesRemainingAndSize(f)