for folder_name in oslistdirsource_dir folder_path = ospathjoinsource_dir folder_name if ospathisdirfolder_path and folder_name0isalpha destination_dir = negative_dir else desti
问题可能出在判断文件夹名称是否以字母开头这一条件上。根据你的描述,中文文件夹名称也被放入了negative_dir,说明条件判断不准确。你可以尝试使用其他方法来判断文件夹名称是否包含中文字符。
以下是一个示例代码,用于判断文件夹名称是否包含中文字符:
import os
import re
def contains_chinese_characters(s):
pattern = re.compile(r'[\u4e00-\u9fa5]')
return pattern.search(s) is not None
for folder_name in os.listdir(source_dir):
folder_path = os.path.join(source_dir, folder_name)
if os.path.isdir(folder_path) and contains_chinese_characters(folder_name):
destination_dir = negative_dir
else:
destination_dir = positive_dir
这里使用了正则表达式来判断文件夹名称中是否包含中文字符。函数contains_chinese_characters会返回一个布尔值,指示字符串中是否包含中文字符。根据该判断结果来确定文件夹的目标路径。
原文地址: http://www.cveoy.top/t/topic/i6lK 著作权归作者所有。请勿转载和采集!