-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubdirs_list.py
104 lines (40 loc) · 1.78 KB
/
subdirs_list.py
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
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 9 11:43:12 2025
@author: varvara
"""
import os
#%% List of subdirs in a dir, containing the string "had_reco" :
def find_subdirs_with_string(root_dir, search_string):
subdirectories = []
for dirpath, dirnames, _ in os.walk(root_dir):
for dirname in dirnames:
if search_string in dirname:
subdirectories.append(os.path.join(dirpath, dirname))
return subdirectories
# Example usage
root_directory = '/path/to/your/directory' # Replace with your root directory path
search_string = 'had_reco'
result = find_subdirs_with_string(root_directory, search_string)
# Print the result
for subdir in result:
print(subdir)
def find_npy_files(subdirectories, search_string):
''' Arguments :
subdirectories : list of strings, full paths
search_string : string to search in file names
'''
npy_files = []
for subdir in subdirectories :
for filename in os.listdir(subdir):
if filename.endswith('.npy') and search_string in filename :
npy_files.append(os.path.join(subdir, filename))
return npy_files
def get_next_n_chars(file_name, char, n):
# Find the index of the given character in the file name
char_index = file_name.find(char)
# If the character is not found, return an empty string
if char_index == -1:
return ''
# Return the next N characters after the given character
return file_name[char_index : char_index + 1 + n]