请选择 进入手机版 | 继续访问电脑版

 找回密码
 立即注册
搜索
热搜: Excel discuz
查看: 1404|回复: 1

[遍历文件夹] scandir分类文件和文件夹,并拿到属性值

[复制链接]

482

主题

7万

元宝

75万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
750844
发表于 2021-8-15 21:38:42 | 显示全部楼层 |阅读模式
  1. import os
  2. dir = []
  3. file = []
  4. for i in os.scandir('d:/迅雷下载'):
  5.     if i.is_file():
  6.         file.append(i.name)
  7.     else:
  8.         dir.append(i.name)
  9. print(file)
  10. print(dir)
复制代码
scandir可以拿到的是可迭代对象,所以文件名字需要加.name。
回复

使用道具 举报

482

主题

7万

元宝

75万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
750844
 楼主| 发表于 2021-8-15 21:43:06 | 显示全部楼层
拿到所有文件的属性值
  1. import os
  2. dir = []
  3. file = []
  4. for i in os.scandir('d:/迅雷下载'):
  5.     if i.is_file():
  6.         print(i.name,i.stat())
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|五花八门论坛 ( 豫ICP备15031300号-3 )

GMT+8, 2024-3-29 15:25 , Processed in 0.114548 second(s), 26 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表