0

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 File_folder folder:

Here is the directory structure:

       App/  
       ├─ main.py
       └─ Files/  
       |   └─B.txt
       |   └─A.xlsx
       |   └─D.xlsx    
       |   └─C.csv
       └─ File_output/

I have multiple functions stored in main.py:

def A_fct(fileA):
    pass
def B_fct(fileB):
    pass
def C_fct(fileC):
    pass
def D_fct(fileD):
    pass
def E_fct(fileE):
    pass
def F_fct(fileF):
    pass

Example:

read file Name B.txt => call B_fct

read file Name A.xlsx => call A_fct

etc ...

How can i do this please!

I have read all the suggestions belows but still don't know how to do it.

Learner
  • 592
  • 1
  • 12
  • 27
  • Does this answer your question? [python function call with variable](https://stackoverflow.com/questions/4431216/python-function-call-with-variable) – Mohammad Aug 26 '21 at 10:37
  • This has already been answered [here](https://stackoverflow.com/questions/4431216/python-function-call-with-variable) where the function call is assigned to a variable – bromate Aug 26 '21 at 10:38
  • I already saw this answer and this is not what i need. I want to Automate the file reading and function applying – Learner Aug 26 '21 at 10:49
  • So what is your question? How to get the file's name? How to pass it to a function? It is not clear – Tomerikoo Aug 26 '21 at 10:56
  • Does this answer your question? [Calling Different Functions in Python Based on Values in a List](https://stackoverflow.com/questions/30293680/calling-different-functions-in-python-based-on-values-in-a-list) – Tomerikoo Aug 26 '21 at 11:01
  • @Tomerikoo, yes how to get the file name than call his adequate function – Learner Aug 26 '21 at 11:23
  • 1
    Which one is it? Those are two questions. SO questions should focus on one. Anyway, both of them are duplicates and already answered on this site. Please make sure to research before asking and only ask here as a last resort – Tomerikoo Aug 26 '21 at 11:25
  • 1
    First this: [How do I list all files of a directory?](https://stackoverflow.com/q/3207219/6045800) then the link above – Tomerikoo Aug 26 '21 at 11:26
  • @Tomerikoo i will check this – Learner Aug 26 '21 at 11:29
  • @Prestige I edited my answer, hope it helps you figure out how to combine the 2 solutions – Almog-at-Nailo Sep 02 '21 at 10:22

1 Answers1

2

If I understand the question correctly, you want to call a certain function for each file depending on the name of the file

you can define a dict with the relationship file_name => function

name_to_func = {
    "A": A_fct,
    "B": B_fct,
    ...
}

then call the function with

name_to_func[file_name](file_name)

Edit:

Now I understand that you want to iterate over files in a folder, you can do that with os.listdir(path).

for example, something like this:

import os

path = '/your/path/here'

name_to_func = {
    "A": A_fct,
    "B": B_fct
}

for file_name in os.listdir(path):
    file_prefix = file_name.split('.')[0]
    name_to_func[file_prefix](file_name)
Almog-at-Nailo
  • 1,152
  • 1
  • 4
  • 20