forked from cclauss/Ten-lines-or-less
-
Notifications
You must be signed in to change notification settings - Fork 0
/
files_and_folders.py
30 lines (23 loc) · 892 Bytes
/
files_and_folders.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import os
# improvement liberally borrowed from:
# https://forum.omz-software.com/topic/2784/feature-request-pythonista-built-in-file-picker
def files_and_folders(dir_path="."):
"""Return a dict containing a sorted tuple of files and a sorted
tuple of folders"""
f_and_f = os.listdir(dir_path)
folders = [f for f in f_and_f if os.path.isdir(os.path.abspath(f))]
files = set(f_and_f) - set(folders)
return {
"files": tuple(sorted(files, key=str.lower)),
"folders": tuple(sorted(folders, key=str.lower)),
}
def old_files_and_folders(dir_path="."):
files = []
folders = []
for filename in sorted(os.listdir(dir_path)):
if os.path.isdir(os.path.join(dir_path, filename)):
folders.append(filename)
else:
files.append(filename)
return tuple(files), tuple(folders)
print(files_and_folders())