Get position for file descriptor in Python

Say I have a raw descriptor for numeric files, and I need to get the current position in the file based on this.

import os, psutil

# some code that works with file
lp = lib.open('/path/to/file')

p = psutil.Process(os.getpid())
fd = p.get_open_files()[0].fd  # int

while True:
    buf = lp.read()
    if buf is None:
        break
    device.write(buf)
    print tell(fd)  # how to find where we are now in the file?

The following code libhas a compiled library that does not allow access to the file object. In a loop, I use a built-in method readthat returns processed data. Data and length are not related to file position, so I cannot mathematically calculate the offset.

I tried to use fdopenhow fd = fdopen(p.get_open_files()[0].fd), but print fd.tell()only returned the first position in the file, which was not updated in the loop.

Is there a way to get the current position in real time in a file based on a file descriptor?

+4
1

, . os.lseek SEEK_CUR:

import os
print(os.lseek(fd, 0, os.SEEK_CUR))

, , , , .

: ftell ?

+1

All Articles