python - Python: best way to dynamically read folder structure and create list of lists
问题描述
So I have this root folder
:
C:\hw
And under this root path I have this folders structure
:
C:\hw\ios
C:\hw\ios\10
C:\hw\ios\11
C:\hw\ios\12
C:\hw\ios\13
C:\hw\ios\14
C:\hw\android
C:\hw\android\9
C:\hw\android\9.0.1
And I want to read all folders
into list
of lists (only first level):
dict = {
'ios': ['10', '11', '12', '13', '14'],
'android': ['9', '9.0.1]
}
Any suggestions ?
解决方案
>>> d = {}
>>> BASE_DIR = "."
>>> for file in os.listdir(BASE_DIR):
... if os.path.isdir(BASE_DIR+file):
... d[file] = os.listdir(BASE_DIR+file)
如果您只想在该目录中添加文件,则可以添加文件检查的额外条件os.path.isfile()
推荐阅读
- python - AttributeError:“selenium”对象在 Selenium 和 Python 中没有“send_keys”属性
- angular6 - Angular 7 + Bootstrap 4 - 预先输入
- java - 我可以在java中使用收集器从文档列表中查找具有最大值的文档吗
- php - 转义 symfony 列名
- python - AttributeError:“psycopg2.extensions.cursor”对象没有属性“fast_executemany”
- ios - 为什么 CGImage 的 copy() 方法会在相同的内存地址上生成图像?
- .net - 当您在可编辑的 ComboBox 外部单击并使其关闭时会触发哪个事件?
- css - css溢出自动不适用于IE11
- tcp - Eiffel Web 框架,在 linux 上运行的应用程序引发“地址已在使用中:IO_FAILURE 引发”
- database - 如何设置 ec2(数据库服务器)与 android 应用程序的连接(用于开发的 android studio)