MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / expanduser

Function expanduser

tools/python-3.11.9-amd64/Lib/ntpath.py:319–364  ·  view source on GitHub ↗

Expand ~ and ~user constructs. If user or $HOME is unknown, do nothing.

(path)

Source from the content-addressed store, hash-verified

317# variable expansion.)
318
319def expanduser(path):
320 """Expand ~ and ~user constructs.
321
322 If user or $HOME is unknown, do nothing."""
323 path = os.fspath(path)
324 if isinstance(path, bytes):
325 tilde = b'~'
326 else:
327 tilde = '~'
328 if not path.startswith(tilde):
329 return path
330 i, n = 1, len(path)
331 while i < n and path[i] not in _get_bothseps(path):
332 i += 1
333
334 if 'USERPROFILE' in os.environ:
335 userhome = os.environ['USERPROFILE']
336 elif not 'HOMEPATH' in os.environ:
337 return path
338 else:
339 try:
340 drive = os.environ['HOMEDRIVE']
341 except KeyError:
342 drive = ''
343 userhome = join(drive, os.environ['HOMEPATH'])
344
345 if i != 1: #~user
346 target_user = path[1:i]
347 if isinstance(target_user, bytes):
348 target_user = os.fsdecode(target_user)
349 current_user = os.environ.get('USERNAME')
350
351 if target_user != current_user:
352 # Try to guess user home directory. By default all user
353 # profile directories are located in the same place and are
354 # named by corresponding usernames. If userhome isn't a
355 # normal profile directory, this guess is likely wrong,
356 # so we bail out.
357 if current_user != basename(userhome):
358 return path
359 userhome = join(dirname(userhome), target_user)
360
361 if isinstance(path, bytes):
362 userhome = os.fsencode(userhome)
363
364 return userhome + path[i:]
365
366
367# Expand paths containing shell variable substitutions.

Callers 1

readSystemPreferencesFunction · 0.90

Calls 6

_get_bothsepsFunction · 0.85
startswithMethod · 0.80
joinFunction · 0.70
basenameFunction · 0.70
dirnameFunction · 0.70
getMethod · 0.45

Tested by

no test coverage detected