-
Notifications
You must be signed in to change notification settings - Fork 0
/
0 - Rename_files_with_lowercase_extension.py
90 lines (71 loc) · 3.28 KB
/
0 - Rename_files_with_lowercase_extension.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
import os
import logging
from pathlib import Path
from shutil import move
################################################################################################
# 1. functions #
################################################################################################
def move_files():
for root, _, files in os.walk(from_directory_path):
for file in files:
file_path = os.path.join(root, file)
if os.path.isfile(file_path):
temp_file = lowercase_extension(file)
temp_file_path = os.path.join(root, temp_file)
rel_name = os.path.relpath(temp_file_path, from_directory_path)
dest_file_path = os.path.join(to_directory_path, rel_name)
os.makedirs(os.path.dirname(dest_file_path), exist_ok=True)
move(file_path, dest_file_path)
def lowercase_extension(filename):
parts = filename.split(".")
modified_parts = [part.lower() if i > 0 else part for i, part in enumerate(parts)]
return ".".join(modified_parts)
################################################################################################
# 2. setup #
################################################################################################
# 1. setup log file path
logfile = r"C:\process_data\log\process.log"
logging.basicConfig(
filename=logfile,
level=logging.INFO,
format="%(message)s - %(asctime)s",
)
# 2. Provide the directory path from which some files have upercase file extension
from_directory_path = r"change me with from_directory_path"
# 3. Provide the destination directory path to which extenstions of all files from 'from_directory_path' and its subdirectories will
# changed to lowercase, then moved
to_directory_path = r"change me with to_directory_path"
################################################################################################
# 3. Rename file extensions with lower case
# 1. Input "from directory path": some files have uppercase file extension
# from_directory_path = r"C:\test0"
# 2. Input "to directory path":
# to_directory_path = r"C:\test1"
# The files from "from_directory_path" and its subdirectories are moved to "to_directory_path" and related subdirectories.
# All files' extentsions should be in lowercase
################################################################################################
def output(msg):
logging.info(msg)
print(msg)
def verify_setup(file_paths, directory_paths):
verified = True
for file_path in file_paths:
if not Path(file_path).is_file():
print(f"File '{file_path}' does not exit.")
verified = False
for directory_path in directory_paths:
if not Path(directory_path).is_dir():
print(f"Directory '{directory_path}' does not exit.")
verified = False
return verified
script_name = "0 - Rename_files_with_lowercase_extension"
output(f"***starting {script_name}")
if verify_setup(
[],
[
from_directory_path,
to_directory_path,
],
):
move_files()
output(f"***completed {script_name}")