Adapting the path/string based on certain values

Viewed 32

The user specifies the structure of the tree to save one or more files as a path (variable dir_depository). But the names of each sub-folders can change according to various parameters. (represented by dict_test here)

import configparser
import os
import re


dir_depository = r"C:\python\protocole\data\param_info"
dir_depository = dir_depository.split("\\")

dict_test = {'protocole' : 'POISSON',
            'data' : 'DATE',
            'param' : 'PRUNE',
            'info' : 'ILE FLOTANTE' }

liste=[]
def rename_path(dir_depository):
    for items in dir_depository:
        if items in dict_test:
            liste.append(dict_test[items])
        elif items == "_":
            liste.append(items)
        # () <- keep the separators
        # [] <- match everything in between
        # ^a-zA-Z0-9: <-except alphabets, upper/lower and numbers  and :  
        elif re.search('[^a-zA-Z0-9:]', items):
            items = re.split('([^a-zA-Z0-9])', items)
            function_a_nommer(items)
        else: 
            liste.append(items)
    return liste

liste = rename_path(dir_depository)

path = os.path.join(*liste)

My goal is to adapt the path values.

input: C:\python\protocol\data\param_info

desired ouput : C:\python\POISSON\DATE\PRUNE_ILE FLOTANT

But for now my output look like this : C:python\POISSON\DATE\PRUNE\_\ILE FLOTANTE

As you can see, I have some problem when it's like to special characters. I've focused on the "_" character for now but it would be nice if it could take into account other special characters.

The « \ » also doesn’t appear between « c : » and « python » and I don’t know why.

0 Answers
Related