我有一个功能:
for root, dirs, files in os.walk(directory):
for file in files:
if fnmatch.fnmatch(file, '*.php*'):
path = os.path.join(root, file)
new = change_file_name(file)
replace_dict[file] = new
os.rename(root + os.sep + file, root + os.sep + new)
但是当In文件中有波兰字符(在本例中为“ż”)时,我收到错误:
Traceback (most recent call last):
File "/root/environments/whois/local/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/root/environments/whois/local/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/root/environments/whois/local/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/root/environments/whois/local/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/root/environments/whois/local/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "./test.py", line 136, in wayback
out = download_wayback(domain,data_od,data_do,timestamp)
File "./wayback.py", line 121, in download_wayback
for root, dirs, files in os.walk(unicode(directory)):
File "/root/environments/whois/lib/python2.7/os.py", line 294, in walk
for x in walk(new_path, topdown, onerror, followlinks):
File "/root/environments/whois/lib/python2.7/os.py", line 294, in walk
for x in walk(new_path, topdown, onerror, followlinks):
File "/root/environments/whois/lib/python2.7/os.py", line 294, in walk
for x in walk(new_path, topdown, onerror, followlinks):
File "/root/environments/whois/lib/python2.7/os.py", line 294, in walk
for x in walk(new_path, topdown, onerror, followlinks):
File "/root/environments/whois/lib/python2.7/os.py", line 284, in walk
if isdir(join(top, name)):
File "/root/environments/whois/lib/python2.7/genericpath.py", line 41, in isdir
st = os.stat(s)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u017c' in position 85: ordinal not in range(128)
有没有办法解决它?我不想改变文件名。