- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想搜索文件,但包含 .txt 文件的文件除外。怎么做 ?目前我的代码正在搜索扩展名为 .txt 的文件。如何做相反的事情?
src = raw_input("Enter source disk location: ")
src = os.path.abspath(src)
print "src--->:",src
for dir,dirs,_ in os.walk(src, topdown=True):
file_path = glob.glob(os.path.join(dir,"*.txt"))
最佳答案
使用列表理解过滤文件:
for dir, dirs, files in os.walk(src):
files = [os.path.join(dir, f) for f in files if not f.endswith('.txt')]
我删除了 topdown=True
参数;这是默认值。
不要将 glob.glob()
与 os.walk()
结合使用;这两种方法都向操作系统查询目录中的文件名。您已经在 os.walk()
每次迭代的第三个值中有了这些文件名。
如果你想跳过整个目录,使用any()
function查看是否有任何匹配的文件,然后使用 continue
忽略此目录:
for dir, dirs, files in os.walk(src):
if any(f.endswith('.txt') for f in files):
continue # ignore this directory
# do something with the files here, there are no .txt files.
files = [os.path.join(dir, f) for f in files]
如果您想忽略此目录及其所有后代,请使用切片分配清除 dirs
变量:
for dir, dirs, files in os.walk(src):
if any(f.endswith('.txt') for f in files):
dirs[:] = [] # do not recurse into subdirectories
continue # ignore this directory
# do something with the files here, there are no .txt files.
files = [os.path.join(dir, f) for f in files]
关于python-2.6 - 如何从 os.walk 中排除文件扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35293399/
不幸的是,这段代码比“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
我是一名优秀的程序员,十分优秀!