I am using fusepy and I need to convert a file descriptor back in to a file object so that I can obtain the original file path
From the fusepy examples, when a file is created, a file descriptor is returned - for example:
def open(self, path, flags):
print("open:", path)
return os.open(path, flags)
the returned result is an integer: <class 'int'>
with the value of 4
in a separate function named write, I need to reverse the file descriptor back in to a file so that I can get the file path, so I tried this:
f = os.fdopen(fh)
When I check the type of f
I get the following f is type: <class '_io.TextIOWrapper'>
Which is not quite what I was expecting but a quick dir(f)
shows that it has a name
property, I thought that's what I was looking for, except name
is simply the number 4
...
How can I get the original file path the descriptor points to?
f.buffer
andf.buffer.raw
, but you still wouldn't get to the name. Not very portable, but at least if on Linux and (I presume at least some) other U*X like systems, a way around could be to ask OS directly, e.g. through procfs structures:os.readlink(f"/proc/self/fd/{fh}")
in your example. – Fixed/proc/self
was a thing either, so +1 for that too (I was at this point prior to your answeros.readlink("/proc/"+str(pid)+"/fd/"+str(fh))
but your example is much cleaner) – Antisepsis