我的脚本搜索它所在的目录,并使用它找到的文件名创建新目录,并将它们移动到该目录:John -doe-taxes.hrb - > John-doe/John-do-taxes.hrb。它工作正常,直到它运行到变音符号然后它将创建目录并返回一个“错误2”,说它找不到该文件。我是相当新的编程,我已经找到了答案,一直到Python。尝试使用文件名创建新目录时发生错误(如果其中包含特殊字符)
coding: utf-8
行添加到,因为我不是在我的代码使用变音符号不工作,我相信我的文件正在处理变音文件。有一件事我很好奇,这个问题是否只是出现在变音符号或其他特殊字符上呢?这是我使用的代码,我很欣赏提供的任何建议。
import os
import re
from os.path import dirname, abspath, join
dir = dirname(abspath(__file__))
(root, dirs, files) = os.walk(dir).next()
p = re.compile('(.*)-taxes-')
count = 0
for file in files:
match = p.search(file)
if match:
count = count + 1
print("Files processed: " + str(count))
dir_name = match.group(1)
full_dir = join(dir, dir_name)
if not os.access(full_dir, os.F_OK):
os.mkdir(full_dir)
os.rename(join(dir, file), join(full_dir, file))
raw_input()
你正在运行什么操作系统? – 2013-03-18 02:25:39
Windows 7 x64 .. – user2163792 2013-03-18 02:32:17
这是什么文件名失败,你得到什么错误? – 2013-03-18 02:53:31