0

I am currently learning Python and looking to build small apps just for practice. I am wanting to build a Windows application that will delete a folder in the C:\users\<username>\appdata\roaming folder. The problem I have is the username will be different depending on what workstation a person is on. I am looking for someone to lead me in the right direction on how I would go about finding this path on each workstation and then deleting a folder within that path. I have looked at relative paths but not sure if I would be able to use that to delete a folder within the %appdata% folder.

wjandrea
  • 28,235
  • 9
  • 60
  • 81

3 Answers3

0

Tell me if I got it wrong. Are you stuck trying to get the username dynamically when run from each account? If so, you can try python's getpass module as below,

import getpass
location = "C:\users\{username}\appdata\roaming".format(username=getpass.getuser())
avrsanjay
  • 805
  • 7
  • 12
  • This is worked great. Now I have to figure out how to to get the function to run until the button is clicked and to delete more than one folder. This is a great site. Thank You for your help!! – smitty68521 May 25 '20 at 19:05
  • @smitty68521 Glad it helped! – avrsanjay May 26 '20 at 04:40
0

You can use os.getlogin() to get the current user and insert it as a placeholder:

import os
currentUser=os.getlogin()
folderPath="C:\\{0}\\<username>\\appdata\\roaming".format(currentUser)
os.rmdir(folderPath)       ----> removes an empty directory.
shutil.rmtree(folderPath)  ----> deletes a directory and all its contents.
CanciuCostin
  • 1,773
  • 1
  • 10
  • 25
0

You can try this to get the appdata path

import os
path = os.getenv('APPDATA')

I found it here.

And after, to delete the folder (in this case I deleted the Zoom folder)

import shutil
folder_to_delete = os.path.join(path,'Zoom')
shutil.rmtree(folder_to_delete, ignore_errors=True)
Eduardo Coltri
  • 506
  • 3
  • 8