Latest web development tutorials

Python3 os.listdir () method

Python3 OS file / directory methods Python3 OS file / directory methods


Outline

List of files or file os.listdir () method returns the specified folder contains a folder name. This list is in alphabetical order. It does not include '' and '..' even if it's in the folder.

Only support the use of Unix, Windows under.

grammar

listdir () method syntax is as follows:

os.listdir(path)

parameter

  • path - the need to list the directory path

return value

Files and return the specified path of the folder list.

Examples

The following example demonstrates listdir () method of use:

#!/usr/bin/python3

import os, sys

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

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

The above program output is:

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

Python3 OS file / directory methods Python3 OS file / directory methods