[英]How best to iterate over a list when a each list item will be passed to a function?
[英]How to automatically iterate over each list item and perform a unique function
我在一个目录中有各种文件;
我想重命名这个目录中的所有文件,但是如果这个目录包含不止一种类型的图像序列,我想一次迭代一个,这样我就不会覆盖任何东西,只会结束最多2个文件。
我打算用文件名的第一部分将它们分开并将它们添加到列表中,以获得变化的数量。 我不确定的是如何让 function 自动迭代 newList[0]、newList[1] 等等。 它需要足够健壮才能满足无限量的列表项。
output 应该是:
下面的代码并不表示重命名任务,它只是开始计划如何以程序方式遍历列表项。
import os
dir = "/test/"
# File formats
imageFileFormats = (".exr", ".dpx")
fileName = []
for file in os.listdir(dir):
for ext1 in imageFileFormats:
if ext1 in file:
fileName.append(file.split('.')[0])
newList = list(set(fileName))
newList.sort()
for file in os.listdir(dir):
for ext1 in imageFileFormats:
if ext1 in file:
if newList[0] in file:
print (file)
这似乎是一个 XY 问题 - 而不是在适当的位置重命名文件集合,从而导致潜在的冲突,您是否考虑过创建一个新文件夹以将所有文件移动到其中,并在此过程中重命名它们? 完成后,您可以将它们移回原来的位置,从而完全避免该问题。
只要每个文件都有从旧名称到新名称的映射,不会导致最终结果发生冲突,那么移动文件的顺序就无关紧要了。
只要目标文件夹在同一卷上,移动操作基本上就是重命名,因此不存在空间问题或性能问题。
所以,像这样:
from pathlib import Path
def do_rename(fn):
# whatever non-conflicting renaming operation you need
p = Path(fn)
return p.parent / f'{p.stem}_renamed{p.suffix}'
def do_reverse_rename(fn):
# just including the reverse of the above here for testing purposes
p = Path(fn)
return p.parent / f'{p.stem[:-8]}{p.suffix}' if p.stem.endswith('_renamed') else p
def safe_rename_all(location, rename_func):
p = Path(location)
# come up with a folder name that doesn't exist, right next to the original
n = 1
while (p.parent / f'{p.name}_{n}').is_dir():
n += 1
# create the new temporary folder
(target := p.parent / f'{p.name}_{n}').mkdir()
# move all the files into the new folder, this example matches *all* files
# of course you could filter, etc.
for fn in p.glob('*'):
new_fn = rename_func(fn)
fn.rename(target / new_fn.name) # move to the temporary location
# once done, move everything back and delete the temporary folder
for fn in target.glob('*'):
fn.rename(p / fn.name)
target.rmdir()
safe_rename_all('some/folder/with/files', do_rename)
# can be undone with safe_rename_all('output', do_reverse_rename)
一些注意事项可能是不要在原始文件旁边创建一个文件夹(以避免权限问题等),而是使用标准库tempfile在同一卷上创建一个临时文件夹。 您过滤了某些后缀,因此很容易添加。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.