简体   繁体   English

python读取文件夹中除名为“ xyz”的文件外的所有文件

[英]python read all files in a folder except a file named “xyz”

I want to read all files in a folder except a file named "xyz". 我想读取文件夹中除名为“ xyz”的文件以外的所有文件。 When I reach to this file, I want to skip it and read the next one. 当我到达此文件时,我想跳过它并阅读下一个文件。

Currently I have the following code: 目前,我有以下代码:

for file in glob.glob('*.xml'):
    data = open(file).read()
    print(file)

Obviously, this will read all files in that folder. 显然,这将读取该文件夹中的所有文件。 How should I skip the file "xyz.xml" 我应该如何跳过文件“ xyz.xml”

The continue keyword is useful for skipping an iteration of a for loop: continue关键字对于跳过for循环的迭代很有用:

for file in glob.glob('*.xml'):
    if file=="xyz.xml":
        continue
    data = open(file).read()
    print(file)
for file in [f for f in glob.glob('*.xml') if f != "xyz.xml"]:
    do_stuff()

Try this, assuming that the element to be removed is in the list returned by glob.glob() (if that's not guaranteed, put the remove() line inside a try block): 尝试执行此操作,假设要删除的元素在glob.glob()返回的列表中(如果不能保证,请将remove()行放在try块中):

lst = glob.glob('*.xml')
lst.remove('xyz.xml') # assuming that the element is present in the list
for file in lst:
    pass

Or if you care about memory usage, use a generator: 或者,如果您关心内存使用情况,请使用生成器:

for file in (file for file in glob.glob('*.xml') if file != 'xyz.xml'):
    pass

For sake of completeness as no one posted the most obvious version: 为了完整起见,没有人发布最明显的版本:

for file in glob.glob('*.xml'):
    if file != 'xyz.xml':
        data = open(file).read()
        print(file)

You can use glob for fairly simple pattern matching but remember that pattern rules for glob are not regular expressions! 您可以使用glob进行非常简单的模式匹配,但请记住,glob的模式规则不是正则表达式! Below code can help you exclude all xml files that start with 'X' 以下代码可以帮助您排除以'X'开头的所有xml文件

files = glob.glob('[!X]*.xml')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python - 如何读取以 xyz 开头的文件夹中的多个文件? - Python - How do you read multiple files in a folder starting by xyz? Python - 读取文件夹中的所有文件,并为每个文件创建唯一的输出 - Python - Read all files in a folder and create unique output for each file 使用Python解析文件夹中除要在XML文件中键入的文件以外的所有文件 - Parse all files in a folder except ones being typed in an XML file, using Python 如何使用 Python 删除文件夹中除最后 5 个项目之外的所有文件 - How delete all files in a folder except last 5 items with Python 读取python文件夹中所有文件的默认函数 - Default function to read all files in a python folder 使用python加载和读取文件夹中的所有文件json - Load and read all files json in a folder with python 从Python文件夹导入和读取所有文件 - import and read all files from a folder Python 蟒蛇; 读取和写入文件夹中的所有文件 - Python; read and write all files in a folder 没有名为“matplotlib”的模块,除了它在那里,同一文件夹中的其他文件还可以,交互式 python 很好 - No module named 'matplotlib' except it's there, other files in same folder ok, interactive python fine python从文件夹中读取所有文件,并将文件名和其他信息写入txt文件 - python read all files from a folder and write the file name and other info into a txt file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM