Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

genericpath.py 4.6 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
  1. """
  2. Path operations common to more than one OS
  3. Do not use directly. The OS specific modules import the appropriate
  4. functions from this module themselves.
  5. """
  6. import os
  7. import stat
  8. __all__ = ['commonprefix', 'exists', 'getatime', 'getctime', 'getmtime',
  9. 'getsize', 'isdir', 'isfile', 'samefile', 'sameopenfile',
  10. 'samestat']
  11. # Does a path exist?
  12. # This is false for dangling symbolic links on systems that support them.
  13. def exists(path):
  14. """Test whether a path exists. Returns False for broken symbolic links"""
  15. try:
  16. os.stat(path)
  17. except OSError:
  18. return False
  19. return True
  20. # This follows symbolic links, so both islink() and isdir() can be true
  21. # for the same path on systems that support symlinks
  22. def isfile(path):
  23. """Test whether a path is a regular file"""
  24. try:
  25. st = os.stat(path)
  26. except OSError:
  27. return False
  28. return stat.S_ISREG(st.st_mode)
  29. # Is a path a directory?
  30. # This follows symbolic links, so both islink() and isdir()
  31. # can be true for the same path on systems that support symlinks
  32. def isdir(s):
  33. """Return true if the pathname refers to an existing directory."""
  34. try:
  35. st = os.stat(s)
  36. except OSError:
  37. return False
  38. return stat.S_ISDIR(st.st_mode)
  39. def getsize(filename):
  40. """Return the size of a file, reported by os.stat()."""
  41. return os.stat(filename).st_size
  42. def getmtime(filename):
  43. """Return the last modification time of a file, reported by os.stat()."""
  44. return os.stat(filename).st_mtime
  45. def getatime(filename):
  46. """Return the last access time of a file, reported by os.stat()."""
  47. return os.stat(filename).st_atime
  48. def getctime(filename):
  49. """Return the metadata change time of a file, reported by os.stat()."""
  50. return os.stat(filename).st_ctime
  51. # Return the longest prefix of all list elements.
  52. def commonprefix(m):
  53. "Given a list of pathnames, returns the longest common leading component"
  54. if not m: return ''
  55. # Some people pass in a list of pathname parts to operate in an OS-agnostic
  56. # fashion; don't try to translate in that case as that's an abuse of the
  57. # API and they are already doing what they need to be OS-agnostic and so
  58. # they most likely won't be using an os.PathLike object in the sublists.
  59. if not isinstance(m[0], (list, tuple)):
  60. m = tuple(map(os.fspath, m))
  61. s1 = min(m)
  62. s2 = max(m)
  63. for i, c in enumerate(s1):
  64. if c != s2[i]:
  65. return s1[:i]
  66. return s1
  67. # Are two stat buffers (obtained from stat, fstat or lstat)
  68. # describing the same file?
  69. def samestat(s1, s2):
  70. """Test whether two stat buffers reference the same file"""
  71. return (s1.st_ino == s2.st_ino and
  72. s1.st_dev == s2.st_dev)
  73. # Are two filenames really pointing to the same file?
  74. def samefile(f1, f2):
  75. """Test whether two pathnames reference the same actual file"""
  76. s1 = os.stat(f1)
  77. s2 = os.stat(f2)
  78. return samestat(s1, s2)
  79. # Are two open files really referencing the same file?
  80. # (Not necessarily the same file descriptor!)
  81. def sameopenfile(fp1, fp2):
  82. """Test whether two open file objects reference the same file"""
  83. s1 = os.fstat(fp1)
  84. s2 = os.fstat(fp2)
  85. return samestat(s1, s2)
  86. # Split a path in root and extension.
  87. # The extension is everything starting at the last dot in the last
  88. # pathname component; the root is everything before that.
  89. # It is always true that root + ext == p.
  90. # Generic implementation of splitext, to be parametrized with
  91. # the separators
  92. def _splitext(p, sep, altsep, extsep):
  93. """Split the extension from a pathname.
  94. Extension is everything from the last dot to the end, ignoring
  95. leading dots. Returns "(root, ext)"; ext may be empty."""
  96. # NOTE: This code must work for text and bytes strings.
  97. sepIndex = p.rfind(sep)
  98. if altsep:
  99. altsepIndex = p.rfind(altsep)
  100. sepIndex = max(sepIndex, altsepIndex)
  101. dotIndex = p.rfind(extsep)
  102. if dotIndex > sepIndex:
  103. # skip all leading dots
  104. filenameIndex = sepIndex + 1
  105. while filenameIndex < dotIndex:
  106. if p[filenameIndex:filenameIndex+1] != extsep:
  107. return p[:dotIndex], p[dotIndex:]
  108. filenameIndex += 1
  109. return p, p[:0]
  110. def _check_arg_types(funcname, *args):
  111. hasstr = hasbytes = False
  112. for s in args:
  113. if isinstance(s, str):
  114. hasstr = True
  115. elif isinstance(s, bytes):
  116. hasbytes = True
  117. else:
  118. raise TypeError('%s() argument must be str or bytes, not %r' %
  119. (funcname, s.__class__.__name__)) from None
  120. if hasstr and hasbytes:
  121. raise TypeError("Can't mix strings and bytes in path components") from None
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...