0

I am creating a small script that removes unwanted browser trackers from the C:\Users\username\AppData\Local\Microsoft\Windows\INetCache location. The problem is I cant seem to get to AppData\Local.

when running this:

os.getenv('APPDATA')

I get this output: C:\Users\username\AppData\Roaming

I need to remove \Roaming so that i can get lower in the \Local tree. How do I dynamically get to the above location exclusively on windows 10 and 7 using python 3.5?

M4dW0r1d
  • 97
  • 3
  • 12
  • `LocalAppData`? – Josh Lee Mar 30 '17 at 16:01
  • this may help you: [Link](http://stackoverflow.com/questions/1038824/how-do-i-remove-a-substring-from-the-end-of-a-string-in-python) – SMFSW Mar 30 '17 at 16:05
  • you can store the result of your code line in a var, then do: var.replace('Roaming', ''), then you can add the rest of the path needed – SMFSW Mar 30 '17 at 16:07

1 Answers1

1

It seems like you should be able to do this using os.path.dirname:

roaming = os.getenv('APPDATA')
app_data = os.path.dirname(roaming)

If you can't be sure to trust the environment variable, but are sure that the path you want will always end in AppData, then you can keep removing path parts until you find the part you want:

app_data = os.getenv('APPDATA')
while app_data and not app_data.endswith('AppData'):
    app_data = os.path.dirname(app_data)
mgilson
  • 300,191
  • 65
  • 633
  • 696