My goal is to create a function that finds all the files of a specific type (extension) in a folder and places them in a list. As arguments of the function I set:
- file type (.xlsx, .png, .txt)
- the list name where the files will be placed
I was able to find all the files of a specific type. I was able to see the files in a list on my terminal I was NOT able to put the files in a list with the name of my choice so that later during the execution I can call the specific list with the name of my choice or any name. Could someone help me solve the problem? thanks in advance for understanding.
def find_all_filles_courrent_dir(type,namelist):
# Η συνάρτηση βρίσκει όλα τα αρχεία τύπου type και τα τοποθετεί σε λίστα με όνομα namelist
import glob
x=str("*")+str(".")+str(type)
namelist = [f for f in glob.glob(x)]
print("Λίστα με τα αρχεία στον υπάρχον φάκελο είναι:",namelist)
find_all_filles_courrent_dir("xlsx","listafor")
print(listafor)
when I run :
> python3 try2.py
Λίστα με τα αρχεία στον υπάρχον φάκελο είναι: ['democreate.xlsx', 'test3.xlsx', 'test2.xlsx', 'test5.xlsx', 'test4.xlsx', 'test1.xlsx', 'teststable.xlsx']
Traceback (most recent call last):
File "try2.py", line 11, in <module>
print(listafor)
NameError: name 'listafor' is not defined
my_var = find_all_files_current_dir('xlsx')Note, you shouldn't uselistas a variable name, because that is used for the built-inlisttype