【发布时间】:2020-02-05 23:04:11
【问题描述】:
我想要一个 Folders_To_Skip.txt 文件,其中包含由新行分隔的目录列表
例如:
A:\\stuff\a\b\
A:\\junk\a\b\
我有一些文件正在破坏我的 .csv 记录编译,我想排除那些我无论如何都不需要阅读的目录。
在locate 函数中,我有我试图从Excluding directories in os.walk 实现的功能,但我似乎无法让它与list 中的目录一起使用,更不用说从文本文件列表中读取的时候了print 访问的文件仍然包含我试图排除的目录中的文件。
您能否解释一下解决方案是特定的排除目录(不是世界末日),还是可以操作排除子目录(会更方便)。
现在locate 之前的代码允许轻松查找控制文本文件,然后将这些项目作为列表加载到脚本的其余部分以运行,假设所有控制文件都在同一位置,但位置可以根据运行脚本的人员和位置而改变。
也出于测试目的,Drive_Locations.txt 设置为:
A
B
这是当前脚本:
import os
from tkinter import filedialog
import fnmatch
input('Press Enter to select any file in writing directory or associated control files...')
fname = filedialog.askopenfilename()
fpath = os.path.split(fname)
# Set location for Drive Locations to scan
Disk_Locations = os.path.join(fpath[0], r'Drive_Locations.txt')
# Set location for Folders to ignore such as program files
Ignore = os.path.join(fpath[0], r'Folders_To_Skip.txt')
# Opens list of Drive Locations to be sampled
with open(Disk_Locations, 'r') as Drives:
Drive = Drives.readlines()
Drive = [x.replace('\n', '') for x in Drive]
# Iterable list for directories to be excluded
with open(Ignore, 'r') as SkipF1:
Skip_Fld = SkipF1.readlines()
Skip_Fld = [x.replace('\n', '') for x in Skip_Fld]
# Locates file in entire file tree from previously established parent directory.
def locate(pattern, root=os.curdir):
for path, dirs, files in os.walk(os.path.abspath(root), topdown=True):
dirs[:] = [d for d in dirs if d not in Skip_Fld]
for filename in fnmatch.filter(files, pattern):
yield os.path.join(path, filename)
for disk in Drive:
# Formats Drive Location for acceptance
disk = str.upper(disk)
if str.find(disk, ':') < 0:
disk = disk + ':'
# Changes the current disk drive
if os.path.exists(disk):
os.chdir(disk)
# If disk incorrect skip to next disk
else:
continue
for exist_csv in locate('*.csv'):
# Skip compiled record output files in search
print(exist_csv)
【问题讨论】:
-
嗨@tv006,欢迎来到该网站。您在这里为我们提供了很多代码,但并不完全清楚您在问什么。在我看来,实现您似乎想要的过滤的最明显的地方是在您正在执行
os.walk调用的locate函数中(它不能在其他任何地方工作),但你没有似乎试图修改dirs。你能从你的例子中删掉一些不相关的代码,或者向我们展示你在目录跳过方面的实际尝试吗? -
您确实需要提取并提供minimal reproducible example。作为新用户,请同时关注tour 并阅读How to Ask。特别是,说“我尝试了一些含糊不清的东西”然后说“它没有用”都没有帮助。提供事实,而不是解释。
-
变量和函数名称应遵循
lower_case_with_underscores样式。不一致的命名和大量的代码使这几乎无法阅读。 -
我还可以看到一堆可以简化的区域,特别是在程序开始时,您正在阅读所有文件。
-
AMC - 不幸的是,我想不出一个解决方案来清理读取文件的部分,因为我正在尝试设置它,所以同事可以在没有任何编码的情况下运行它,只需检查他们是否想要更改我认为对用户更友好的文本文件。我认为该部分可能受益最多的是某种方式来杀死 Windows 资源管理器搜索弹出的
tk窗口。
标签: python