编程语言
首页 > 编程语言> > python使用os库列出指定路径下文件和子目录

python使用os库列出指定路径下文件和子目录

作者:互联网

参考链接: os.listdir(path=’.’)

在这里插入图片描述

实验代码:

import os
folderPath = r'D:\Python\Python35\Lib\site-packages\jieba'
dirList = os.listdir(folderPath)
print(type(dirList))
print(len(dirList))
print(dirList)
print(type(dirList[0]))

控制台下输出:

Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。

尝试新的跨平台 PowerShell https://aka.ms/pscore6

加载个人及系统配置文件用了 827 毫秒。
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\my-experiments> conda activate pytorch_1.7.1_cu102
(pytorch_1.7.1_cu102) PS C:\Users\chenxuqi\Desktop\News4cxq\my-experiments>  & 'D:\Anaconda3\envs\pytorch_1.7.1_cu102\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2021.1.502429796\pythonFiles\lib\python\debugpy\launcher' '51011' '--' 'c:\Users\chenxuqi\Desktop\News4cxq\my-experiments\test.py'
<class 'list'>
8
['analyse', 'dict.txt', 'finalseg', 'posseg', '_compat.py', '__init__.py', '__main__.py', '__pycache__']
<class 'str'>
(pytorch_1.7.1_cu102) PS C:\Users\chenxuqi\Desktop\News4cxq\my-experiments>

标签:__,dirList,Users,chenxuqi,python,子目录,Desktop,os
来源: https://blog.csdn.net/m0_46653437/article/details/115872750