i tend to copy a lot of paths from a windows file explorer address bar, and they can get pretty long:
C:\Users\avnav\Documents\Desktop\SITES\SERVERS\4. server1\0_SERVER_MAKER
now let's say i want to replace avnav
with getpass.getuser()
.
if i try something like:
project_file = f'C:\Users\{getpass.getuser()}\Documents\Desktop\SITES\SERVERS\4. server1\0_SERVER_MAKER'
i will get the following error:
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
i presume it's because of the single back slash - although would be cool to pinpoint exactly what part causes the error, because i'm not sure.
in any case, i can also do something like:
project_file = r'C:\Users\!!\Documents\Desktop\SITES\SERVERS\4. server1\0_SERVER_MAKER'.replace('!!', getpass.getuser())
but this becomes annoying once i have more variables.
so is there a solution to this? or do i have to find and replace slashes or something?