小白教程
所有教程
关于
Search
172.70.126.50
172.70.126.50
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Python3 os listdir”的源代码
本页内容
上一节:
Python3_os_link
下一节:
Python3_os_lseek
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:os.listdir(path)}}[[Category:python3 os file methods|28]] = Python3 os.listdir() 方法 = [[python3 os file methods|Python3 OS 文件/目录方法]] === 概述 === os.listdir() 方法用于返回指定的文件夹包含的文件或文件夹的名字的列表。这个列表以字母顺序。 它不包括 . 和 .. 即使它在文件夹中。 只支持在 Unix, Windows 下使用。 === 语法 === '''listdir()'''方法语法格式如下: <sample title="" desc="" lang="python" hererun="1"> os.listdir(path) </sample> === 参数 === * '''path''' -- 需要列出的目录路径 === 返回值 === 返回指定路径下的文件和文件夹列表。 == 以下实例演示了 listdir() 方法的使用: <sample title="" desc="" lang="python" hererun="1"> #!/usr/bin/python3 import os, sys # 打开文件 path = "/var/www/html/" dirs = os.listdir( path ) # 输出所有文件和文件夹 for file in dirs: print (file) </sample> 执行以上程序输出结果为: <sample title="" desc="" lang="python" hererun="1"> test.htm stamp faq.htm _vti_txt robots.txt itemlisting resumelisting writing_effective_resume.htm advertisebusiness.htm papers resume </sample> 目录中存在中文的情况不需要特别处理,也可以正常输出,因为 Python3 默认是 utf8 编码。目录结构如下: <sample title="" desc="" lang="python" hererun="1"> #!/usr/bin/python- import os # 打开文件 path = "./git-test" upath = unicode(path,'utf-8') dirs = os.listdir( upath ) # 输出所有文件和文件夹 for file in dirs: print (file) </sample> 执行以上程序输出结果为: <sample title="" desc="" lang="python" hererun="1"> xiaobai xiaobai-git-test another-xiaobai-name 中文目录测试 </sample> [[python3 os file methods|Python3 OS 文件/目录方法]]
返回至“
Python3 os listdir
”。
上一节:
Python3_os_link
下一节:
Python3_os_lseek