test123

有了前一篇文章的介绍,再输出目录,也不过是多写一个函数的事情了,我把它封装成了类~~

发现walk()真的是一个超级方便好用的函数。这种情况下用listdir()是搞定不了的啦

import os

class GetPath:
@staticmethod
def get_dir(path): # 获取目录路径
print("所有目录路径是:")
for root, dirs, files in os.walk(path): # 遍历path及每个目录,有3个参数,root表示目录路径,dirs表示当前目录的目录名,files代表当前目录的文件名
for dir in dirs:
print(os.path.join(root, dir))

@staticmethod
def get_file(path): # 获取文件路径
print("所有文件路径是:")
for root, dirs, files in os.walk(path):
for file in files:
print(os.path.join(root, file))

if __name__ == \'__main__\':
path =r"D:\python workspace\py11\1001"
GetPath.get_dir(path)
GetPath.get_file(path)

 

结果:

 

分类:

技术点:

相关文章:

  • 2022-01-07
  • 2021-11-07
  • 2021-09-28
  • 2021-11-07
  • 2021-11-20
  • 2021-10-20
  • 2021-11-23
  • 2022-01-07
猜你喜欢
  • 2021-11-23
  • 2021-10-13
  • 2021-11-20
  • 2021-09-11
  • 2021-12-23
  • 2021-09-11
  • 2021-11-01
相关资源
相似解决方案