【问题标题】:How can I make a conditional loop in python while skipping some files and read sequentially?如何在跳过某些文件并按顺序读取的同时在 python 中创建条件循环?
【发布时间】:2021-02-16 10:54:25
【问题描述】:

#我想创建一个循环,因为我的图像文件夹中有数千张图像(图像名称类似于 t000001xy1c1.tif、t000001xy1c2.tif、t000001xy1c3.tif)。如何操作以下脚本来制作一些东西,以便我可以从列表中顺序跳过每第二张图像。##

##generating gzip file (black images), 
##keeping it in the same directory and deleting the raw file##

import gzip
import os


#input file name

in_file = "t000001xy1c4.tif"

#reading input file

in_data = open(in_file, "rb").read()

#output file format (making gzip file and deleting the in_file)

out_gz = "t000001xy1c4.tif.gz"

gzf = gzip.open(out_gz, "wb")

gzf.write(in_data)

gzf.close()

#delete the original file after the gzip is done##

os.unlink(in_file)

【问题讨论】:

  • 此脚本用于压缩文件。如果您想使用文件列表,请开始搜索示例以获取目录中的所有文件。以 os.listdir 为例。
  • 谢谢。其实我是这方面的初学者。我想压缩我的文件并删除原始文件,但由于文件名很大并且文件夹包含数千张图像,因此难以读取文件。我正在寻找 os.listdir 和任何其他与我的查询相关的建议将不胜感激。
  • 这里stackoverflow.com/questions/3207219/… 有很多例子。如果您不必保持图像无损,将它们转换为 jpg 会更方便。
  • 感谢您的协助。我只是困惑,但我知道它很简单。从 listdir 我得到类似的输出 - T000001XY01C2 T000001XY02C2 T000001XY03C2 T000001XY04C2 ………。我想制作一个循环,以便跳过一个并处理另一张图像。如果文件名是1,2,3,4我可以做到……这种类型的文件名怎么做。
  • 我已经编写了我将在答案中使用的解决方案。我希望它会有所帮助。

标签: python loops image-scaling


【解决方案1】:

根据您的评论,您已经知道如何使用 listdir。我自己总是使用以下功能。它可以一次搜索多个扩展名,例如 ['.tif', '.jpg']。

import os
import re

def get_all_file_paths(dir_path, extensions=[]):
    found_file_paths = []
    dir_entries = os.listdir(dir_path)
    for dir_entry in dir_entries:
        dir_entry_path = os.path.join(dir_path, dir_entry)
        if os.path.isfile(dir_entry_path):
            if extensions:
                # accept files with required extensions --------------------------
                extension = os.path.splitext(dir_entry)[1]
                if extension.lower() in extensions:
                    found_file_paths.append(dir_entry_path)
            else:
                # accept all files -----------------------
                found_file_paths.append(dir_entry_path)

    return found_file_paths


file_paths = get_all_file_paths(r'C:\dir', extensions=['.tif'])

假设它返回一个这样的列表

file_paths = [
    r'C:\dir\T000001XY01C2.tif',
    r'C:\dir\T000001XY02C2.tif',
    r'C:\dir\T000001XY03C2.tif',
    r'C:\dir\T000001XY04C2.tif',
    ]

然后我会使用正则表达式来提取你想要的数字。

for file in file_paths:

    # get the XY**C*. part
    re_xy_c = r'XY\d+C\d+\.'
    result = re.findall(re_xy_c, os.path.split(file)[1])
    if result:
        xy_c_string = result[0]
        # now get the numbers from the xy_c_string
        re_numbers = r'\d+'
        result = re.findall(re_numbers, xy_c_string)
        first_nr = int(result[0])
        second_nr = int(result[1])
        print(first_nr, second_nr)
        # now make your choice
        if first_nr % 2 == 0:
            # even nr
            print('do even')
        else:
            # odd nr
            print('skip odd')
        print('------------')

结果

1 2
skip odd
------------
2 2
do even
------------
3 2
skip odd
------------
4 2
do even
------------

使用可以在这里测试你的正则表达式regex101.com

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多