How to get temp directory in Python with a non-ascii windows username?

Viewed 153

Simple question, but can't get to find the answer (maybe wrong google search term ?).

My Windows 10 temp directory is

C:\Users\Aurélien\AppData\Local\Temp

Notice my username: Aurélien contains an é which isn't standard ascii compatible. But hey that's my name.

When using Interactive Python 3.8 within VS 2019, I type this code:

import tempfile
print(tempfile.gettempdir())

and it results in:

C:\Users\AURLIE~1\AppData\Local\Temp

How to get the proper formatting answer?

Expected

C:\Users\Aurélien\AppData\Local\Temp

Thanks.

1 Answers

Here is my hideous "workaround", it is an answer for now, but I won't tick it as accepted answer. If you have better one I'll glady wait for it.

from pathlib import Path
def _getutempdir():
  usr = 'Users'
  h = Path.home()
  tmp = Path(tempfile.gettempdir())
  lh = list(h.parts)
  ltmp = list(tmp.parts)
  if (usr in lh and usr in ltmp):
      hui = lh.index(usr)
      tmpui = ltmp.index(usr)
      if ((len(lh)> hui+1) and (len(ltmp)>tmpui+1)):
          ltmp[tmpui+1] = lh[hui+1]
          tmp = Path(*ltmp)
  return tmp
Related