鱼C论坛

 找回密码
 立即注册
查看: 1916|回复: 2

[已解决]python文件夹

[复制链接]
发表于 2022-9-4 14:59:10 | 显示全部楼层 |阅读模式
6鱼币
path1 = r"C:\Users\22536\Downloads\main\moxing"
如何返回路径中的所有文件夹,如何moxing这个文件夹
最佳答案
2022-9-4 14:59:11


利用 os 模块,参考代码:
import os

path1 = r"C:\Users\22536\Downloads\main\moxing"
dir, file = [], []

for i in os.listdir(path1):
    if os.path.isdir(path1 + os.sep + i):
        dir.append(i)
    else:
        file.append(i)


print(path1 + "  路径下的文件夹:", *dir)
print(path1 + "  路径下的文件:", *file)

os 模块速查手册:https://fishc.com.cn/forum.php?mod=viewthread&tid=45512

最佳答案

查看完整内容

利用 os 模块,参考代码: os 模块速查手册:https://fishc.com.cn/forum.php?mod=viewthread&tid=45512
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-9-4 14:59:11 | 显示全部楼层    本楼为最佳答案   


利用 os 模块,参考代码:
import os

path1 = r"C:\Users\22536\Downloads\main\moxing"
dir, file = [], []

for i in os.listdir(path1):
    if os.path.isdir(path1 + os.sep + i):
        dir.append(i)
    else:
        file.append(i)


print(path1 + "  路径下的文件夹:", *dir)
print(path1 + "  路径下的文件:", *file)

os 模块速查手册:https://fishc.com.cn/forum.php?mod=viewthread&tid=45512

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-9-4 15:14:39 | 显示全部楼层
import os

path1 = r"C:\Users\22536\Downloads\main\moxing"

# 每循环一次 root 就深入一层目录
for root, dirs, files in os.walk(path1):
    # dirs 是 root 中的所有文件夹,files 是 root 中的所有文件
    for d in  dirs: 
        print(os.path.join(root, d))
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-15 03:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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