You can use the os
module in Python to obtain all direct subfolders of a given directory using the os.listdir()
method. Here is an example:
import os
path = '/path/to/folder'
subfolders = [f.path for f in os.scandir(path) if f.is_dir()]
This will return a list of all subfolders within the specified directory. If you want only the names of the subfolders rather than their full paths, you can modify the list comprehension as follows:
subfolders = [f.name for f in os.scandir(path) if f.is_dir()]
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2022-01-30 11:00:00 +0000
Seen: 5 times
Last updated: Nov 09 '21
How can I set up Gunicorn with a Django Project?
Looking for a Python Module that finds Tags for a Text describing its Content
Need a Function in Python to remove entries less than 2 digits from an Array
How can I convert a Document in Python?
How can I program a Loop in Python?
How can I enable Python Code Highlighting in Askbot?