• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

python 显示目录下的文件夹和文件的方法

python 水墨上仙 1693次浏览

python读取目录下的内容,包含子目录和文件夹

#!/usr/bin/env python
#
# [代码名字: List Directories Content]
# [代码分类: os]
# [代码描述: List the content of the home directory]
# [代码作者: Andy Breiner <breinera@gmail.com>]
# [代码协议: GPL]
# [SNIPPET_DOCS: http://docs.python.org/library/os.html, http://diveintopython.org/file_handling/os_module.html]
import os
# expand ~ to /home/
# also print out the content of the home directory as a list
print os.listdir(os.path.expanduser("~"))
# Loop over all the items and determine if they are a file or directory and
# then print them out
directory = os.path.expanduser("~")
for f in os.listdir(directory):
    if os.path.isfile(os.path.join(directory, f)):
        print "File: " + f
    if os.path.isdir(os.path.join(directory, f)):
        print "Directory: " + f

 


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明python 显示目录下的文件夹和文件的方法
喜欢 (0)
加载中……