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

pathex.py 3.3 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
  1. from pathlib import Path
  2. from os import scandir
  3. image_extensions = [".jpg", ".jpeg", ".png", ".tif", ".tiff"]
  4. def write_bytes_safe(p, bytes_data):
  5. """
  6. writes to .tmp first and then rename to target filename
  7. """
  8. p_tmp = p.parent / (p.name + '.tmp')
  9. p_tmp.write_bytes(bytes_data)
  10. if p.exists():
  11. p.unlink()
  12. p_tmp.rename (p)
  13. def scantree(path):
  14. """Recursively yield DirEntry objects for given directory."""
  15. for entry in scandir(path):
  16. if entry.is_dir(follow_symlinks=False):
  17. yield from scantree(entry.path) # see below for Python 2.x
  18. else:
  19. yield entry
  20. def get_image_paths(dir_path, image_extensions=image_extensions, subdirs=False, return_Path_class=False):
  21. dir_path = Path (dir_path)
  22. result = []
  23. if dir_path.exists():
  24. if subdirs:
  25. gen = scantree(str(dir_path))
  26. else:
  27. gen = scandir(str(dir_path))
  28. for x in list(gen):
  29. if any([x.name.lower().endswith(ext) for ext in image_extensions]):
  30. result.append( x.path if not return_Path_class else Path(x.path) )
  31. return sorted(result)
  32. def get_image_unique_filestem_paths(dir_path, verbose_print_func=None):
  33. result = get_image_paths(dir_path)
  34. result_dup = set()
  35. for f in result[:]:
  36. f_stem = Path(f).stem
  37. if f_stem in result_dup:
  38. result.remove(f)
  39. if verbose_print_func is not None:
  40. verbose_print_func ("Duplicate filenames are not allowed, skipping: %s" % Path(f).name )
  41. continue
  42. result_dup.add(f_stem)
  43. return sorted(result)
  44. def get_paths(dir_path):
  45. dir_path = Path (dir_path)
  46. if dir_path.exists():
  47. return [ Path(x) for x in sorted([ x.path for x in list(scandir(str(dir_path))) ]) ]
  48. else:
  49. return []
  50. def get_file_paths(dir_path):
  51. dir_path = Path (dir_path)
  52. if dir_path.exists():
  53. return [ Path(x) for x in sorted([ x.path for x in list(scandir(str(dir_path))) if x.is_file() ]) ]
  54. else:
  55. return []
  56. def get_all_dir_names (dir_path):
  57. dir_path = Path (dir_path)
  58. if dir_path.exists():
  59. return sorted([ x.name for x in list(scandir(str(dir_path))) if x.is_dir() ])
  60. else:
  61. return []
  62. def get_all_dir_names_startswith (dir_path, startswith):
  63. dir_path = Path (dir_path)
  64. startswith = startswith.lower()
  65. result = []
  66. if dir_path.exists():
  67. for x in list(scandir(str(dir_path))):
  68. if x.name.lower().startswith(startswith):
  69. result.append ( x.name[len(startswith):] )
  70. return sorted(result)
  71. def get_first_file_by_stem (dir_path, stem, exts=None):
  72. dir_path = Path (dir_path)
  73. stem = stem.lower()
  74. if dir_path.exists():
  75. for x in sorted(list(scandir(str(dir_path))), key=lambda x: x.name):
  76. if not x.is_file():
  77. continue
  78. xp = Path(x.path)
  79. if xp.stem.lower() == stem and (exts is None or xp.suffix.lower() in exts):
  80. return xp
  81. return None
  82. def move_all_files (src_dir_path, dst_dir_path):
  83. paths = get_file_paths(src_dir_path)
  84. for p in paths:
  85. p = Path(p)
  86. p.rename ( Path(dst_dir_path) / p.name )
  87. def delete_all_files (dir_path):
  88. paths = get_file_paths(dir_path)
  89. for p in paths:
  90. p = Path(p)
  91. p.unlink()
Tip!

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

Comments

Loading...