- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一些简单的代码可以打印出目录的结构。我的示例目录 ABC
包含包含 A.txt
的子目录 A
,包含 Z.txt 的子目录
和一个文件 Z
info.txt
。在实际使用中,这将是许多文件和嵌套目录的大集合。
import os
topdir = 'ABC/'
for dirpath, dirnames, files in os.walk(topdir):
print(os.path.join(dirpath))
for name in files:
print(os.path.join(dirpath, name))
输出是:
ABC/
ABC/info.txt
ABC/A
ABC/A/A.txt
ABC/Z
ABC/Z/Z.txt
我怎样才能使目录在顶部被处理/打印?我希望输出复制我在 Windows 资源管理器中看到的内容,它首先显示目录,然后显示文件。
我想要的输出:
ABC/
ABC/A
ABC/A/A.txt
ABC/Z
ABC/Z/Z.txt
ABC/info.txt
最佳答案
如果不将所有文件存储在一个列表中并以一种或另一种方式对该列表进行排序,您可以创建一个递归函数并在打印当前级别的文件之前首先递归到目录结构的下一级:
def print_dirs(directories):
try:
dirpath, dirnames, files = next(directories)
print(dirpath) # print current path; no need for join here
for _ in dirnames: # once for each sub-directory...
print_dirs(directories) # ... recursively call generator
for name in files: # now, print files in current directory
print(os.path.join(dirpath, name))
except StopIteration:
pass
print_dirs(os.walk(topdir))
堆栈也可以这样做,但我认为这样更清晰一些。是的,这也会将一些目录存储在列表/堆栈中,但不是所有文件,而是与嵌套目录级别一样多。
编辑:这在生成器上打印 any 下一个目录时出现问题,即使那不是子目录而是兄弟目录(或“叔叔”或其他)。 for _ in dirnames
循环应该解决这个问题,对每个子目录(如果有的话)进行一次递归调用。目录本身不必作为参数传递,因为它将从生成器中获取。
关于python - os.walk 但目录在顶部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54399251/
不幸的是,这段代码比“os.walk”运行得慢,但为什么呢? 会不会是“for”循环导致运行缓慢? “像‘os.walk’一样工作的代码:(“os.walk”函数做它做的事) 注意:我写作是为了提升自
已关闭。这个问题是 off-topic 。目前不接受答案。 想要改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 已关闭11 年前。 Improve th
我不确定我是否能正确表达这个问题,但就是这样...... 我想编写一个示例,其中小点具有它们移动所依据的速度 - 而且,还有一个随机运动叠加到“正确”运动上。使用下面的 Processing 代码,我
我正在写 Walk function in the go tutorial基本上按顺序遍历树。我的作品: package main import ( "fmt" "code.googl
我是 AngularJS 的新手,但我真的很喜欢 AngularJS 的工作方式,所以我想将它部署为我的 Google 云端点后端的客户端。然后我立即遇到两个问题: 1、放在哪里myCallback
使用 python 3.5 时 os.walk()并查看 dirname 输出,我得到了很多空列表(返回 [ ] )。我不知道为什么。 import os tdir = '/home/pontiac/
我终于让我的 Files.walk 开始工作了,我的问题是是否有任何方法可以识别收集到列表中的文件是否来自子文件夹或主文件夹,因为这些文件有删除功能,但来自子文件夹的文件用户不应删除文件夹。 pri
我需要在 os.walk 进程中获取当前目录。当只有一个子目录级别时它可以工作,但当有更多子目录级别时它会失败。请指教... [代码] # AFFECTS everything reachable f
当我 snmpWalk 一个 ip 时,需要 5 秒。但如果我在 for 循环中发送 snmpWalk 请求,步行时间就会增加。以多线程方式使用 snmp 或仅使用 ExecutorService 是
我正在尝试计算大量父目录中特定文件夹中的文件数量。每个项目都包含相同的文件夹结构: C:/Projects 2019 Project A Communi
我需要迭代文件服务器上的所有文件,并且我希望能够停止该进程并稍后在文件树中的任何位置恢复它。这可以用 os.walk 来完成,还是需要从头开始实现? 编辑:理想情况下,我希望解决方案是持久的,以便脚本
我有一个包含数千个文件的文件夹。我需要将每个文件的文件名与另一个数组中的列进行匹配。 import os filenames = [] for files in os.walk("Directory"
我想打开一个文件夹中的一系列子文件夹,找到一些文本文件并打印文本文件的一些行。我正在使用这个: from glob import glob import fileinput with open('ou
我是 stackoverflow 的新手。在编写以下代码时,我从该论坛获得了很多帮助。下面的代码搜索系统驱动器上的所有目录/子目录,但在查找“D”驱动器时,它只查找我运行该程序的文件夹后面的那些目录和
我有一个文件夹,其中有对应10个不同类的子文件夹,这些子文件夹的名称就是我的标签。我想出了以下代码将图像读入 Numpy 数组并保存标签。 import numpy as np import pand
我目前正在尝试编写一个简单的Python程序,该程序循环遍历一堆子目录,查找java文件并打印一些有关某些关键字使用次数的信息。我已经设法让这个在大部分情况下工作。我遇到的问题是打印有关更高目录的总体
我想遍历一个目录并搜索给定的文件。这是我编写的一些代码: import os def find(filename, path): for root, dirs, files in os.walk(
我不确定这种方法在 C 中是否正确(详细为 Atmel xmega 的 avrgcc)。数据是 ASCII 字符串。 #define BUFFER_SIZE 64 char dataBuffer[2
使用 Unity2017.3.1f1 Personal(64 位)为 Android 构建 VR 应用程序,使用 Cardboard VR SDK。该应用的目的是让用户以身临其境的方式可视化数据。 目
这是我要解决的问题。我有一个简单的 HTML 页面: Hello World
我是一名优秀的程序员,十分优秀!