flet - Android scan folder for files - Stack Overflow

admin2025-04-17  4

Given a folder path from a file picker, I would like to get all folders and files within this folder. I managed to do it on Windows, but not on Android (only folders are detected).

I tried os.scandir(), os.listdir() from os, and pathlib in the get_files_in_folder function. Could I please have some help? Please find below the app I am trying to run.

Code sample import flet as ft import os

import pathlib

def get_files_in_folder(page): # get all folders in folder path page.files={} for k in page.folder_path.keys(): path = pathlib.Path(k) # Get all files and directories all_entries = [child.name for child in path.iterdir()] page.files[k] = all_entries page.update()

def main(page: ft.Page): page.folder_path={}

def pick_files_result(e: ft.FilePickerResultEvent, page = page):
    if e.path:
        page.folder_path[e.path] = e.path
        selected_path.value = f"path: {e.path}"

        get_files_in_folder(page)
        files_in_path.value = f"path: {page.files[e.path]}"
        page.update()
        
pick_files_dialog = ft.FilePicker(on_result=pick_files_result)

# Register the file picker with the page
page.overlay.append(pick_files_dialog)

floating_action_button = ft.FloatingActionButton(
    content=ft.Row(
        [ft.Icon(ft.icons.ADD)], alignment="center", spacing=5
    ),
    shape=ft.CircleBorder(),
    width=100,
    mini=True,
    on_click = lambda _: pick_files_dialog.get_directory_path()
)    
selected_path = ft.Text(value="No directory selected")
files_in_path=ft.Text(value="No files")


page.add(floating_action_button)
page.add(selected_path)
page.add(files_in_path)

Run the Flet app

ft.app(target=main)

转载请注明原文地址:http://anycun.com/QandA/1744828814a88190.html