Truncate File With A Path Relative To A Directory/file Descriptor?
Many methods of the os module (see documentation) support paths relative to file descriptors (dir_fd), for example the unlink method: os.unlink(path, *, dir_fd=None) I have been r
Solution 1:
truncate
does not have a dir_fd
parameter, because there is no truncateat
system call, which would be required for that. See discussion here.
The proper and only feasible solution actually is:
deftruncate(path, length, dir_fd = None):
fd = os.open(path, flags = os.O_WRONLY, dir_fd = dir_fd)
os.ftruncate(fd, length)
os.close(fd)
Differing from my initial question, one MUST NOT specify mode os.O_TRUNC
when opening the file. If one does that, the file will be truncated to zero by just opening it, which is by no means intended.
Post a Comment for "Truncate File With A Path Relative To A Directory/file Descriptor?"