Python os.listdir() 方法

Python 教程 · 2019-02-03 23:43:00

概述

os.listdir() 方法用于返回指定的文件夹包含的文件或文件夹的名字的列表。这个列表以字母顺序。 它不包括 '.' 和'..' 即使它在文件夹中。

只支持在 Unix, Windows 下使用。

语法

listdir()方法语法格式如下:

os.listdir(path)

参数

  • path -- 需要列出的目录路径

返回值

返回指定路径下的文件和文件夹列表。

实例

以下实例演示了 listdir() 方法的使用:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys

# 打开文件
path = "/var/www/html/"
dirs = os.listdir( path )

# 输出所有文件和文件夹
for file in dirs:
   print file

执行以上程序输出结果为:

test.htm
stamp
faq.htm
_vti_txt
robots.txt
itemlisting
resumelisting
writing_effective_resume.htm
advertisebusiness.htm
papers
resume

点击查看所有 Python 教程 文章: https://www.codercto.com/courses/l/8.html

查看所有标签

Network Algorithmics,

Network Algorithmics,

George Varghese / Morgan Kaufmann / 2004-12-29 / USD 75.95

In designing a network device, you make dozens of decisions that affect the speed with which it will perform - sometimes for better, but sometimes for worse. "Network Algorithmics" provides a complete......一起来看看 《Network Algorithmics,》 这本书的介绍吧!

SHA 加密
SHA 加密

SHA 加密工具

html转js在线工具
html转js在线工具

html转js在线工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具