Python Forum
using dir_fd=None in some functions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
using dir_fd=None in some functions
#1
in The Python Library Reference i see this:
Output:
You can check whether or not dir_fd is supported on your platform using os.supports_dir_fd. If it is unavailable, using it will raise a NotImplementedError.
my question is, does specifying dir_fd=None count as not using it? or must that option name really need to be absent to count as not using it?

i am creating a function the will be passed a path and could use a keyword option dir_fd. i am wondering if is is OK to default it to None and just pass its value along to os.lstat(...,dir_fd=dir_fd,...) or do i need to be more elaborate to handle these cases in a portable way, such as calling os.lstat() in two different ways.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Use try/except block

with os imported:

>>> import os
>>> os.supports_dir_fd
{<built-in function mknod>, <built-in function open>, <built-in function symlink>, <built-in function access>, <built-in function chown>, <built-in function mkfifo>, <built-in function readlink>, <built-in function rename>, <built-in function rmdir>, <built-in function utime>, <built-in function link>, <built-in function mkdir>, <built-in function unlink>, <built-in function chmod>, <built-in function stat>}
>>>
without os imported:

>>> try:
...     os.supports_dir_fd
... except NameError:
...     print("Did You forgot to import os?")
... 
Did You forgot to import os?
>>>
Reply
#3
(Jun-13-2024, 11:33 PM)Skaperen Wrote: my question is, does specifying dir_fd=None count as not using it? or must that option name really need to be absent to count as not using it?
This is implied by the function's documentation. The signature os.lstat(path, *, dir_fd=None) means that dir_fd defaults to None. It means that not using dir_fd it is equivalent to using None. You need to check the doc of each function for which you want to do this.
« We can solve any problem by introducing an extra level of indirection »
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020