-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_utils.py
53 lines (40 loc) · 1.3 KB
/
file_utils.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
#!/usr/bin/python
"""Library class for file utils."""
import glob
import os
import sys
class FileUtils(object):
"""FileUtils class to manage files and folders."""
def __init__(self, dry_run=False):
self._dry_run = dry_run
def get_folders(self, folder_pattern):
"""Gets a list of folders that match the folder_pattern.
Args:
folder_pattern: str, can be a specific folder or with '*'
Returns:
a list of str.
"""
if not folder_pattern:
print('The folder must be set')
sys.exit(1)
folders = []
if '*' in folder_pattern:
folders = glob.glob(folder_pattern)
else:
folders = [folder_pattern]
if not len(folders):
print('No folders exist for {}'.format(folder_pattern))
sys.exit(1)
return folders
def delete_folder_if_empty(self, folder):
"""Deletes a folder if it's empty.
Args:
folder: str, the fully qualified folder path.
"""
num_of_files = len(os.listdir(folder))
if num_of_files == 0:
print("Deleting {} becaust it's empty".format(folder))
if not self._dry_run:
os.rmdir(folder)
return True
return False