To avoid the syntax errors caused by special characters in file names like ( and :, we can escape the file names properly for the cp command in bash. Instead of using replace, a better approach is to use shlex.quote() from Python’s standard library, which handles escaping special characters automatically.
Example code:
import shlex
from pathlib import Path
exp_list = ['run-20240927_204642-67m7tapk_sod_remi', 'run-20241001_011233-lsnkf97x_sod_melody500', 'run-20241006_002237-p8islog7_sod_melody500_noBPEreg']
generated_folder_name_list = ['generated_samples_for_listening_test_None_threshold_0.99_temperature_1.0', 'generated_samples_for_listening_test_top_p_threshold_0.99_temperature_1.0', 'generated_samples_for_listening_test_top_p_threshold_0.95_temperature_1.0']
tune_name_list = ['Concerto Grosso No.6, 1st mvt: Vivace_orch', 'concertos_bwv-1060_3_orch', 'Sonata for Strings, 1st mvt: Affetuoso (arr.)_orch', 'symphony_38_504_2_orch']
target_folder_name_list = ['no_sampling_method', 'top_p_0.99', 'top_p_0.95']
# Get mp3, png, mid files that start with the tune name
# Copy those files into the target folder
for exp in exp_list:
exp_dir = Path('wandb') / exp
for generated_folder_name, target_folder_name in zip(generated_folder_name_list, target_folder_name_list):
generated_dir = exp_dir / generated_folder_name
for tune_name in tune_name_list:
for file_type in ['mp3', 'png', 'mid']:
files = list(generated_dir.glob(f'{tune_name}*.{file_type}'))
# Delete the file which contains 'prompt' in the file name
files = [file for file in files if 'prompt' not in file.name]
if len(files) == 1:
file = files[0]
target_dir = Path('/home/jude/userdata/symbolic-music-encoding/listening_test_for_advisor1008') / exp / target_folder_name
target_dir.mkdir(exist_ok=True, parents=True)
# Use shlex.quote() to escape special characters in file names
source_file = shlex.quote(str(file))
target_file = target_dir / file.name
target_file_quoted = shlex.quote(str(target_file))
# Use escaped file names in the cp command
!cp {source_file} {target_file_quoted}