-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenameRemoveOps.py
More file actions
31 lines (27 loc) · 1.18 KB
/
RenameRemoveOps.py
File metadata and controls
31 lines (27 loc) · 1.18 KB
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
import os
import fnmatch
import shutil
# Remove folder and all its subfolders, then create the folder again
def cleanup_dir(folder):
if os.path.exists(folder):
print('Cleanup folder {}'.format(folder))
shutil.rmtree(folder, ignore_errors=False)
print('Create new folder {}'.format(folder))
os.makedirs(folder)
# Rename a directory or file
def rename(source, destination):
print(' Rename ' + source)
print(' to ' + destination)
os.rename(source, destination)
# Recursively remove set of files.
# target_dir: Absolute directory from which files need to be removed, e.g. D:/Sources/
# file_list: provide a common seperated values with regex patterns matching file names, e.g. ['*.Doc*', '*proj*']
def remove_files(target_dir, file_list):
for rootDir, subdirs, filenames in os.walk(target_dir):
for oneFilepattern in file_list:
for filename in fnmatch.filter(filenames, oneFilepattern):
try:
os.remove(os.path.join(rootDir, filename))
print('Deleting file -'+ os.path.join(rootDir, filename))
except OSError:
print("Error while deleting file")