2

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
TRex
  • 127
  • 1
  • 8
  • Does this help: https://stackoverflow.com/questions/28101187/deal-with-unicode-usernames-in-python-mkdtemp ? – Yuval.R Jun 13 '21 at 04:36
  • @Gravity and @Yuval.R I was stepped upon this on my forehead research, and yeah it is very similar to my point, except, as far as I understand (so maybe I don't), it is an issue for python 2.7. As myself running under python 3.x (3.8), I tried the workaround (using `unicode()`). But it isn't available in Python 3.x as, if I'm correct, in 3.x unicode should be standard behavior ? I tried also using `from builtins import str` as a python 3 replacement for unicode(), but didn't change the result path. – TRex Jun 13 '21 at 07:32

1 Answers1

0

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
TRex
  • 127
  • 1
  • 8