Skip to content

Commit 7c89bf1

Browse files
committed
Move new functions from "iotools" to "pathtools"
Since they're not dealing with I/O but with directories and files they are much better kept here.
1 parent 4907864 commit 7c89bf1

2 files changed

Lines changed: 76 additions & 76 deletions

File tree

src/imcflibs/iotools.py

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -107,79 +107,3 @@ def readtxt(fname, path="", flat=False):
107107
if zipread is not None:
108108
zipread.close()
109109
return txt
110-
111-
112-
def list_dirs_containing_filetype(source, filetype):
113-
"""List all directories (recursive) that contain files with the given suffix.
114-
115-
Parameters
116-
----------
117-
source : str
118-
Path to source dir.
119-
filetype : str
120-
Filetype (string pattern) that should be matched against filenames in
121-
the directories.
122-
123-
Returns
124-
-------
125-
list(str)
126-
List of all dirs that contain files of the given filetype.
127-
"""
128-
dirs_containing_filetype = []
129-
130-
# walk recursively through all directories
131-
# list their paths and all files inside (=os.walk)
132-
for dirname, _, filenames in os.walk(source):
133-
# stop when encountering a directory that contains "filetype"
134-
# and store the directory path
135-
for filename in filenames:
136-
if filetype in filename:
137-
dirs_containing_filetype.append(dirname + "/")
138-
break
139-
140-
return dirs_containing_filetype
141-
142-
143-
def list_all_filenames(source, filetype):
144-
"""Get a sorted list of all files of specified filetype in a given directory.
145-
146-
Parameters
147-
----------
148-
source : str
149-
Path to the directory to scan for desired files.
150-
filetype : str
151-
The file extension to look for.
152-
153-
Returns
154-
-------
155-
list(str)
156-
List of all files with the given suffix in the source directory.
157-
"""
158-
os.chdir(str(source))
159-
allimages = sorted(glob.glob("*" + filetype)) # sorted by name
160-
161-
return allimages
162-
163-
164-
def get_folder_size(source):
165-
"""Get the total size of a given directory and its subdirectories.
166-
167-
Parameters
168-
----------
169-
source : str
170-
Directory for which the size should be determined.
171-
172-
Returns
173-
-------
174-
int
175-
The total size of all files in the source dir and subdirs in bytes.
176-
"""
177-
total_size = 0
178-
for dirpath, _, filenames in os.walk(source):
179-
for fname in filenames:
180-
fpath = os.path.join(dirpath, fname)
181-
# skip if it is symbolic link
182-
if not os.path.islink(fpath):
183-
total_size += os.path.getsize(fpath)
184-
185-
return total_size

src/imcflibs/pathtools.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,82 @@ def derive_out_dir(in_dir, out_dir):
263263
return out_dir
264264

265265

266+
def list_dirs_containing_filetype(source, filetype):
267+
"""List all directories (recursive) that contain files with the given suffix.
268+
269+
Parameters
270+
----------
271+
source : str
272+
Path to source dir.
273+
filetype : str
274+
Filetype (string pattern) that should be matched against filenames in
275+
the directories.
276+
277+
Returns
278+
-------
279+
list(str)
280+
List of all dirs that contain files of the given filetype.
281+
"""
282+
dirs_containing_filetype = []
283+
284+
# walk recursively through all directories
285+
# list their paths and all files inside (=os.walk)
286+
for dirname, _, filenames in os.walk(source):
287+
# stop when encountering a directory that contains "filetype"
288+
# and store the directory path
289+
for filename in filenames:
290+
if filetype in filename:
291+
dirs_containing_filetype.append(dirname + "/")
292+
break
293+
294+
return dirs_containing_filetype
295+
296+
297+
def list_all_filenames(source, filetype):
298+
"""Get a sorted list of all files of specified filetype in a given directory.
299+
300+
Parameters
301+
----------
302+
source : str
303+
Path to the directory to scan for desired files.
304+
filetype : str
305+
The file extension to look for.
306+
307+
Returns
308+
-------
309+
list(str)
310+
List of all files with the given suffix in the source directory.
311+
"""
312+
os.chdir(str(source))
313+
allimages = sorted(glob.glob("*" + filetype)) # sorted by name
314+
315+
return allimages
316+
317+
318+
def get_folder_size(source):
319+
"""Get the total size of a given directory and its subdirectories.
320+
321+
Parameters
322+
----------
323+
source : str
324+
Directory for which the size should be determined.
325+
326+
Returns
327+
-------
328+
int
329+
The total size of all files in the source dir and subdirs in bytes.
330+
"""
331+
total_size = 0
332+
for dirpath, _, filenames in os.walk(source):
333+
for fname in filenames:
334+
fpath = os.path.join(dirpath, fname)
335+
# skip if it is symbolic link
336+
if not os.path.islink(fpath):
337+
total_size += os.path.getsize(fpath)
338+
339+
return total_size
340+
341+
266342
# pylint: disable-msg=C0103
267343
# we use the variable name 'exists' in its common spelling (lowercase), so
268344
# removing this workaround will be straightforward at a later point

0 commit comments

Comments
 (0)