all
This commit is contained in:
@@ -1,20 +1,48 @@
|
||||
import os
|
||||
|
||||
def show_all_files_directory(dir_path = str, search_endung = False, search_string = False):
|
||||
li_all = []
|
||||
def show_all_files_directory(dir_path: str, search_endung: str = None, search_string: str = None) -> list:
|
||||
"""
|
||||
Returns a list of all files in the directory and its subdirectories.
|
||||
|
||||
Parameters:
|
||||
dir_path (str): Path to the root directory.
|
||||
search_endung (str): Ending for the file names to be searched. Default is None.
|
||||
search_string (str): String to be searched in the file names. Default is None.
|
||||
|
||||
Returns:
|
||||
list: List of full paths to all files matching the search criteria.
|
||||
"""
|
||||
|
||||
# Check if dir_path is not empty and exists
|
||||
if not os.path.isdir(dir_path):
|
||||
print("Die angegebene Verzeichnispfad existiert nicht.")
|
||||
return []
|
||||
|
||||
all_files_path = []
|
||||
|
||||
for root_folder, dirs, files in os.walk(dir_path, topdown=False):
|
||||
for name in files:
|
||||
FullPATH = str(os.path.join(root_folder, name))
|
||||
if not search_endung is False:
|
||||
if FullPATH.endswith(search_endung):
|
||||
li_all.append(FullPATH)
|
||||
elif not search_string is False:
|
||||
if not FullPATH.find(search_string) == -1:
|
||||
li_all.append(FullPATH)
|
||||
else:
|
||||
li_all.append(FullPATH)
|
||||
return li_all
|
||||
|
||||
full_path = str(os.path.join(root_folder, name))
|
||||
|
||||
# If search_endung is specified
|
||||
if search_endung:
|
||||
if not full_path.endswith(search_endung):
|
||||
continue
|
||||
|
||||
# If search_string is specified
|
||||
elif search_string:
|
||||
if full_path.find(search_string) == -1:
|
||||
continue
|
||||
|
||||
all_files_path.append(full_path)
|
||||
|
||||
print("Dateien:")
|
||||
for file in all_files_path:
|
||||
print(file)
|
||||
|
||||
print(show_all_files_directory(dir_path="/home/jonnybravo/Downloads", search_string=".txt"))
|
||||
return all_files_path
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test = show_all_files_directory(dir_path="/home/jonnybravo/Downloads", search_endung=".txt")
|
||||
print(test)
|
||||
Reference in New Issue
Block a user