added remove_trailing_spaces_from_filenames()

This commit is contained in:
2023-03-01 00:18:35 +00:00
parent 3cb38bfd7d
commit cd77e205f3
2 changed files with 13 additions and 1 deletions

View File

@@ -3,6 +3,17 @@ from utils.extractor import extract_file_to_dir
BAD_DIR_NAME = '__BAD__'
def remove_trailing_spaces_from_filenames(gradebook_dir):
for filename in os.listdir(gradebook_dir):
if BAD_DIR_NAME not in filename:
name, ext = os.path.splitext(filename) # separate file name from extension
if name != name.rstrip():
new_filename = name.rstrip() + ext # remove trailing spaces from file name and combines with extension
new_file_path = os.path.join(gradebook_dir, new_filename)
old_file_path = os.path.join(gradebook_dir, filename)
os.rename(old_file_path, new_file_path) # rename file
def validate_gradebook_dir_name(src_dir):
if not os.path.isdir(src_dir): # check if it exists and is a directory
print(f"\n[Error] Incorrect directory: {src_dir}\n[Info] Make sure the directory exists in 'BB_gradebooks'")