Parameters description¶
-
ospa.
listdir
(path='.', full_path=True, only_files=False, walk=False, extensions=None) → list[source]¶ Generate items list of directory
Parameters: - path (str) – source path
- full_path (bool) – if True, return full path of files.
- only_files (bool) – if True, return only files without dir (os.path.isfile() is used).
- walk (bool) – generate the file names in a directory tree by walking the tree top-down, using os.walk().
- extensions (Iterable) – list, tuple or set with file extensions.
Returns: files or/and dirs in path
Usage samples¶
All samples are with dummy_test_folder (you can find it in repo) which structure is:
dummy_test_folder/
├─antigravity.png
├─egg.png
├─empty.txt
├─holy_grenade.png
├─spam.jpg
├─txt_files/
| ├─1.txt
| ├─2.txt
| ├─3.txt
| └-not_txt.not_txt
└-memes/
├─meme1.jpg
├─meme2.png
├─meme4.jpg
├─meme4.png
└-meme monty python/
├─meme1.jpg
├─meme2.jpg
└-meme3.jpg
Get all files in directory (without dirs) with absolute path¶
Filter dir items and return only files with full path:
import ospa dummy_folder = ‘/path/to/dummy_test_folder/’ files = ospa.listdir(dummy_folder, only_files=True)
The result is:
['/path/to/dummy_test_folder/antigravity.png',
'/path/to/dummy_test_folder/egg.png',
'/path/to/dummy_test_folder/empty.txt',
'/path/to/dummy_test_folder/holy_grenade.png',
'/path/to/dummy_test_folder/spam.jpg']
Get all file names in directory (without dirs)¶
Filter dir items and return only file names:
import ospa
dummy_folder = '/path/to/dummy_test_folder/'
files = ospa.listdir(dummy_folder, full_path=False, only_files=True)
The result is:
['antigravity.png',
'egg.png',
'empty.txt',
'holy_grenade.png',
'spam.jpg']
Get all files and directories in directory with absolute path¶
Return all dir items with absolute path:
import ospa
dummy_folder = '/path/to/dummy_test_folder/'
files = ospa.listdir(dummy_folder)
The result is:
['/path/to/dummy_test_folder/antigravity.png',
'/path/to/dummy_test_folder/egg.png',
'/path/to/dummy_test_folder/empty.txt',
'/path/to/dummy_test_folder/holy_grenade.png',
'/path/to/dummy_test_folder/spam.jpg',
'/path/to/dummy_test_folder/memes',
'/path/to/dummy_test_folder/txt_files']
The same as os.listdir¶
The behaviour is exactly the same as os.listdir()
:
import ospa
dummy_folder = '/path/to/dummy_test_folder/'
files = ospa.listdir(dummy_folder, full_path=False)
The result is:
['antigravity.png',
'egg.png',
'empty.txt',
'holy_grenade.png',
'spam.jpg',
'memes',
'txt_files']
Get all images in directory and its subdirectories¶
You can use extensions
for getting all specific files.
Extensions
list can contains dot or not and not sensitive to register:
import ospa
dummy_folder = '/path/to/dummy_test_folder/'
files = ospa.listdir(dummy_folder, only_files=True,
walk=True, extensions=['png', 'jpg'])
The result is:
['/path/to/dummy_test_folder/antigravity.png',
'/path/to/dummy_test_folder/egg.png',
'/path/to/dummy_test_folder/holy_grenade.png',
'/path/to/dummy_test_folder/spam.jpg',
'/path/to/dummy_test_folder/memes/meme1.jpg',
'/path/to/dummy_test_folder/memes/meme2.png',
'/path/to/dummy_test_folder/memes/meme4.jpg',
'/path/to/dummy_test_folder/memes/meme4.png',
'/path/to/dummy_test_folder/memes/meme monty python/meme1.jpg',
'/path/to/dummy_test_folder/memes/meme monty python/meme2.jpg',
'/path/to/dummy_test_folder/memes/meme monty python/meme3.jpg']