python--遍历当前文件夹下的目录和文件

os.walk()遍历所有目录和文件

os.walk()使用格式参考:菜鸟教程

os.walk(top, topdown=True, οnerrοr=None, followlinks=False)

top – 是你所要遍历的目录的地址, 返回的是一个三元组(root,dirs,files)。
root 所指的是当前正在遍历的这个文件夹的本身的地址
dirs 是一个 list ,内容是该文件夹中所有的目录的名字(不包括子目录)
files 同样是 list , 内容是该文件夹中所有的文件(不包括子目录)

通常使用的是top参数,其他的参数可选默认。遍历类似二叉树中的层序遍历,先遍历当前文件夹下的一级目录和文件,然后依次遍历每一个一级目录。一级目录的遍历会更类似先序遍历,一个一级目录遍历结束,再去遍历另一个一级目录。具体可看程序结果。
参考:
Python入门之os.walk()方法
当前路径为E:\test,其目录结构为:

E:\test
|--------1.txt
|--------2.txt
|--------test1
		|--------3.txt
		|--------4.txt
|--------test2
		|--------5.txt
		|--------6.txt

遍历并打印当前路径下的目录和文件:

import os
def func():
    print(os.sep)  # 显示当前平台下文件路径分隔符
    # fileDir = "E:" + os.sep + "test"
    fileDir = os.sep.join(["E:","test"])  # 以分隔符连接路径名
    for root, dirs, files in os.walk(fileDir):  
        print('the path is ...')
        print(root)  
        print('the current directories under current directory :')
        print(dirs)  
        print('the files in current directory :')
        print(files)
        print('')
           
if __name__ == "__main__":
    func()  

结果:

\
the path is ...
E:\test
the current directories under current directory :
['test1', 'test2']
the files in current directory :
['1.txt', '2.txt']

the path is ...
E:\test\test1
the current directories under current directory :
[]
the files in current directory :
['3.txt', '4.txt']

the path is ...
E:\test\test2
the current directories under current directory :
[]
the files in current directory :
['5.txt', '6.txt']

从结果可以看出,遍历过程是一个一个目录遍历,每次遍历都得到一个三元组(root,dirs,files),而且dirs和files都是一个列表,包含所有的目录或者文件。我们可以通过遍历该列表获取其中的每一个元素。
例如,遍历获取所有的目录或者文件的绝对路径:

import os
def func():
    print(os.sep)  # 显示当前平台下文件路径分隔符
    # fileDir = "E:" + os.sep + "test"
    fileDir = os.sep.join(["E:","test"])  # 以分隔符连接路径名
    for root, dirs, files in os.walk(fileDir):  
        print('print the absolute path of the directory...')
        for dir in dirs:  
            print(os.path.join(root, dir))  
        
        print('print the absolute path of the file...')
        for file in files:  
            print(os.path.join(root, file))  
        print('')
           
if __name__ == "__main__":
    func()  

结果如下:

print the absolute path of the directory...
E:\test\test1
E:\test\test2
print the absolute path of the file...
E:\test\1.txt
E:\test\2.txt

print the absolute path of the directory...
print the absolute path of the file...
E:\test\test1\3.txt
E:\test\test1\4.txt

print the absolute path of the directory...
print the absolute path of the file...
E:\test\test2\5.txt
E:\test\test2\6.txt

os.listdir()遍历当前路径下目录和文件

os.listdir()默认只能获取当前路径下的目录和文件,如果要获取子目录,需要进一步判断再遍历。

import os
def func():
    fileDir = os.sep.join(["E:","test"])  # 以分隔符连接路径名
    fileList = os.listdir(fileDir)
    for file in fileList:
        print(file)

if __name__ == "__main__":
    func()

结果如下:

1.txt
2.txt
test1
test2
  • 4
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值