[英]How can I make a conditional loop in python while skipping some files and read sequentially?
#I want to make a loop as in my images folder there are thousands of images (image name is like t000001xy1c1.tif, t000001xy1c2.tif, t000001xy1c3.tif). #我想创建一个循环,因为在我的图像文件夹中有数千张图像(图像名称类似于 t000001xy1c1.tif、t000001xy1c2.tif、t000001xy1c3.tif)。 How can I manipulate the following scripts to make something so that I can skip every 2nd images sequentially from a list.##
我如何操作以下脚本来制作一些东西,以便我可以从列表中依次跳过每第二张图片。##
##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)
According to your comment you already figured out how to use listdir.根据您的评论,您已经知道如何使用 listdir。 I myself always use the following function.
我自己总是使用以下功能。 It can search on multiple extensions at once like ['.tif', '.jpg'].
它可以一次搜索多个扩展名,如 ['.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'])
Suppose it returns a list like this假设它返回一个这样的列表
file_paths = [
r'C:\dir\T000001XY01C2.tif',
r'C:\dir\T000001XY02C2.tif',
r'C:\dir\T000001XY03C2.tif',
r'C:\dir\T000001XY04C2.tif',
]
Then I would use regular expressions to extract the numbers you want.然后我会使用正则表达式来提取你想要的数字。
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('------------')
Result结果
1 2
skip odd
------------
2 2
do even
------------
3 2
skip odd
------------
4 2
do even
------------
Use can test your regular expressions here regex101.com使用可以在这里测试您的正则表达式regex101.com
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.