简体   繁体   中英

Get folder location for windows dynamically in python 3.5

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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM