Reputation: 499
I have a folder path which has some cyrillic letter which stop the process of another script. I need to get a folder path without cyrillic letters but python doesn't return it.
The folder is C:\Users\PereverzevPV\Documents\Инв
.
I have:
import os
folder = os.getcwd()
print folder
The output is empty no matter what coding I set. I need to get
'C:\\Users\\PereverzevPV\\Documents\\\xd0\x98\xd0\xbd\xd0\xb2'
Upvotes: 0
Views: 112
Reputation: 963
try this :
# coding: UTF-8
import os
os.getcwd().encode('utf-8').decode('utf-8')
We Need To Encode The CWD then to Decode Him
Upvotes: 1
Reputation: 151
If you pass a file path in unicode than a return will also be in unicode:
>>> for dirpath, dirnames, filenames in os.walk(u"D:\\SO"):
print dirnames
[u'\u0142', u'\u0418\u043d\u0432']
Maybe you could use that and later reassemble the path as you want it.
Upvotes: 1
Reputation: 411
This might help. Your poblem is, that you are not escaping \
: \U
, \P
, \D
or \И
are not valid utf-8 chars.
# coding: UTF-8
txt = r"C:\Users\PereverzevPV\Documents\Инв"
txt = "C:\\Users\\PereverzevPV\\Documents\\Инв"
esc_txt = txt.encode('utf-8')
print(esc_txt)
Upvotes: 0