舒蒂尔无法移动包含特殊字符的文件


can't open/read file: check file path/integrity

所以每当我的文件名中有一个特殊字符时,我都无法移动文件。Shutil.Copy不是一个选项,因为我有太多的文件。

是否有其他库或其他解决方案?

def run():
#resize
all_files = [f for f in listdir("E:\Images\path\") if isfile(join("E:\Images\path\", f))]
for file in all_files:
try:
im = cv2.imread("E:\Images\path\" + file)
width = im.shape[0]
height = im.shape[1]
ratio = width - height
print(width, height)
if abs(ratio) < 1000:
shutil.move(f"E:\Images\path\{file}", f"E:\Images\path\Square\{file}")

elif width < height:
shutil.move(f"E:\Images\path\{file}", f"E:\Images\path\Horizontal\{file}")
elif width > height:
shutil.move(f"E:\Images\path\{file}", f"E:\Images\path\Vertical\{file}")
except:
print('error')

run((

请记住,我没有办法对此进行测试,因此请注意空

这种方法极大地简化了路径名处理,如果出现任何错误,还会发出有意义的错误消息。

然而,有一种假设是,您只对JPG文件感兴趣。如果这不正确,那么你需要做一些小的调整。

import glob
import os
import cv2
import shutil
BASE = 'E:\Image\path'
for file in glob.glob(os.path.join(BASE, '*.jpg')):
try:
im = cv2.imread(file)
width = im.shape[0]
height = im.shape[1]
ratio = abs(width-height)
if ratio < 1_000:
target = 'Square'
else:
target = 'Horizontal' if width < height else 'Vertical'
shutil.move(file, os.path.join(BASE, target, os.path.basename(file)))
except Exception as e:
print(f'Failed to process {file} due to {e}')

最新更新