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

@@ -1,5 +1,5 @@
import os, sys
from utils.organiser import organise_gradebook, check_submissions_dir_for_compressed
from utils.organiser import organise_gradebook, check_submissions_dir_for_compressed, remove_trailing_spaces_from_filenames
def main():
gradebook_name = ' '.join(sys.argv[1:]) if len(sys.argv) > 1 else exit(f'\nNo gradebook name given. Provide the name as an argument.\n\nUsage: python {sys.argv[0]} [gradebook dir name]\n')
@@ -9,6 +9,7 @@ def main():
abs_path = os.getcwd() # absolute path of main/this script
print(f'\nGradebook directory to organise: {os.path.join(abs_path, gradebook_dir)}')
remove_trailing_spaces_from_filenames(gradebook_dir)
organise_gradebook(gradebook_dir, submissions_dir)
check_submissions_dir_for_compressed(submissions_dir)

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'")