I want to read file name(A/B/C/D) and call the convenable function for each file in Files folder and then process the next file (pass automatically to the next file and function).
I have multiple files stored in Files folder:
Here is the directory structure:
App/
├─ main.py
└─ Files/
└─B.txt
└─A.xlsx
└─folderC
| └─filec1.txt
| └─filec2.txt
└─D.xlsx
└─folderF
└─fileF1.txt
└─fileF2.txt
I have multiple functions stored in main.py:
def A_fct(fileA):
pass
def B_fct(fileB):
pass
def C_fct(fileC1,fileC2):
pass
def D_fct(fileD):
pass
def F_fct(files):
df = pd.concat([pd.read_csv(f,sep='\t',engine="python") for f in
glob.glob(files + "/*.txt")],ignore_index=True)
NB: Somes functions have 2 arguments.
Example:
read file Name B.txt => call B_fct
read file Name A.xlsx => call A_fct
etc ...
I tried this solution:
path = ''
name_to_func = {
"A.xlsx": A_fct,
"B.txt": B_fct,
...
}
for files in os.listdir(path):
name_to_func[file_name](files)
This solution is worked very well for me when functions have only one argument but sometimes I have exceptions and I have a fonction with 2 arguments for Exp C_fct(fileC1,fileC2)
How can I fix this please!