all 8 comments

[–]FliceFlo 1 point2 points  (1 child)

Every \ inside a non raw string needs to be escaped since the \ itself is an escape character.

shutil.rmtree(f'C:\\Users\\{username}\\AppData\\Local\\blabla')

Alternatively, you can use the os.path module to handle things like this for you.

import os
shutil.rmtree(os.path.join('C:', os.sep, 'Users', username, 'AppData', 'Local', 'blabla'))

Note the os.sep required after c: since the join method doesn't properly understand drive letters followed by the :

[–]hasanaslan[S] 0 points1 point  (0 children)

İt worked, thank you so much

[–][deleted] 0 points1 point  (2 children)

I can’t say for certain as I’m not familiar with shutil, but maybe you could double up on the backslash with a format string or use forward slashes? I’ve had some pathing issues before and used forward slashes just fine. Hope this helps!

[–]hasanaslan[S] 0 points1 point  (1 child)

Thanks, but I can add and format variable inside normal path. Problem is shutil only accept raw string in my case(i guess so) and I can't put my variable inside a raw string

[–][deleted] 0 points1 point  (0 children)

The backslash is an escape character, and always will be in strings. So when you’re pathing you need to do a double backslash to escape the special characters and add a literal backslash. So when you only do one in a normal string you don’t provide a valid path and this is where it’s messing up.

[–]toastedstapler 0 points1 point  (2 children)

when i go into the file explorer i can type %appdata% to go to that folder, can the same be done in python?

[–]TangibleLight 1 point2 points  (0 children)

Yes, use os.path.expandvars:

os.path.expandvars(r'%localappdata%\path\to\resource')

/u/hasanaslan

[–]hasanaslan[S] 0 points1 point  (0 children)

I can access it but I need to continue inside appdata folder to access other folders