Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
which is "better"?
#5
I wonder if it would be better to use the stat module's symbolic constants. This would do a os.stat() call for each path, but I don't know how many system calls your code does
import os
import stat

codes = [
    (stat.S_ISLNK, 'l'),
    (stat.S_ISBLK, 'b'),
    (stat.S_ISCHR, 'c'),
    (stat.S_ISDIR, 'd'),
    (stat.S_ISREG, 'f'),
    (stat.S_ISSOCK, 's'),
    (stat.S_ISFIFO, 'p'),
]

def pathftn(path):
    mode = os.stat(str(path), follow_symlinks=False).st_mode
    for func, letter in codes:
        if func(mode):
            return letter
    return '?'
Reply


Messages In This Thread
which is "better"? - by Skaperen - Mar-31-2018, 12:34 AM
RE: which is "better"? - by DeaD_EyE - Mar-31-2018, 10:36 AM
RE: which is "better"? - by wavic - Mar-31-2018, 02:26 PM
RE: which is "better"? - by Budsy - Mar-31-2018, 06:26 PM
RE: which is "better"? - by Gribouillis - Apr-01-2018, 07:08 AM
RE: which is "better"? - by Skaperen - Apr-02-2018, 04:29 AM
RE: which is "better"? - by ljmetzger - Apr-02-2018, 09:45 AM
RE: which is "better"? - by Gribouillis - Apr-02-2018, 10:10 AM
RE: which is "better"? - by Skaperen - Apr-03-2018, 02:47 AM

Forum Jump:

User Panel Messages

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