Python 文件名中的 €

Python € in filename

我需要能够读取或重命名包含以下字符的文件:€。 这个角色有一个随机位置,我用 a.我该怎么做?

import os
os.rename("C:\Python2\"+u"€.txt","test.txt")

发给我:

Traceback (most recent call last):
  File "test.py", line 21, in <module>
    os.rename("C:\Python2\"+u"Ôé¼.txt","test.txt")

将“€”替换为“\u20ac”。

这是欧元符号的 unicode。

mytext = "C:\Python2\\u20ac.txt"
print(mytext)

结果:

C:\Python2\€.txt

编辑 Python 2:

eurosign = u"\u20AC"
mytext = "C:\Python2\" + eurosign + ".txt"
print(mytext)

结果:

C:\Python2\€.txt

使用str.decode方法:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import os
os.rename("C:/Python2/€.txt".decode('UTF-8'),"test.txt")