Think before you speak, read before you think.

python获取某目录下的所有文件和子目录-os.listdir

有时候需要获取某目录下的所有文件和子目录,从stackoverflow搜到了一段代码,需要时可以使用。

#!/usr/bin/python

import os, sys

# Open a file
path = "/var/www/html/"
dirs = os.listdir(path)

# This would print all the files and directories
for file in dirs:
   print file

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *