0

I have created a python 3.4 script to delete files in a foler(Temp files o fWindows Folder)

Here it is but it is not working

import os, subprocess
del_dir = r'C:\Users\vaibhav2\AppData\Local\Temp'
pObj = subprocess.Popen('del /S /Q /F %s\\*.*' % del_dir, shell=True, 
stdout = subprocess.PIPE, stderr= subprocess.PIPE)
rTup = pObj.communicate()
rCod = pObj.returncode
if rCod == 0:
    print ('Success: Cleaned Windows Temp Folder')
else:
    print('Fail: Unable to Clean Windows Temp Folder')

EDIT

Some of the code has been edited The error I am facing is that it deletes only some files instead of ALL files in %tmp% folder of Windows ,even if I run the script as Admin. If it requires authentication please provide the code for providing password to this command of admin because I can't do right click run as admin every time

Thanks in advance

adiga
  • 34,372
  • 9
  • 61
  • 83
nothing1
  • 23
  • 5
  • Do you have permissions to do so. I do not think you have permissions. What error do you get? – Elis Byberi Nov 07 '17 at 18:47
  • No error at all – nothing1 Nov 07 '17 at 18:48
  • this is what I get Process finished with exit code 0 – nothing1 Nov 07 '17 at 18:48
  • How to get Permissions ? I am logged in Windows as Admin + there is no password on the admin user – nothing1 Nov 07 '17 at 18:49
  • Can you delete files manually? If you can not delete them manually, you can not delete them with Python either. – Elis Byberi Nov 07 '17 at 18:49
  • Run script as Admin. Give it a try. – Elis Byberi Nov 07 '17 at 18:50
  • I tried using Admin I ran cmd using admin then typed python filename.py still no effect – nothing1 Nov 07 '17 at 18:57
  • Can u add the code to run the script using Admin – nothing1 Nov 07 '17 at 18:58
  • Can you run script `run as Admin` without cmd? – Elis Byberi Nov 07 '17 at 19:04
  • Question updated sorry about inconvenience – nothing1 Nov 07 '17 at 19:06
  • Try listing the directory with `os.listdir`, deleting files with `os.remove`, and recursively deleting subdirectories with `shutil.rmtree`. You'll have more control and direct feedback regarding any Windows errors that get raised. – Eryk Sun Nov 08 '17 at 06:48
  • I see you originally had something close to the above suggestion, before changing it to use the shell. However, your original code incorrectly used the base name `f` instead of the fully-qualified name `p = os.path.join(del_dir, f)`. – Eryk Sun Nov 08 '17 at 06:53
  • please edit that question body, its irritating. – Ankush Rathi Nov 09 '17 at 18:45
  • Please do not vandalize your posts. Your edit invalidates the comments and answers posted. By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the [CC BY-SA 3.0 license](https://creativecommons.org/licenses/by-sa/3.0/)). By SE policy, any vandalism will be reverted. If you would like to disassociate this post from your account, see [What is the proper route for a disassociation request?](https://meta.stackoverflow.com/q/323395) – adiga Nov 09 '17 at 19:14

1 Answers1

0

The problem is that "f" is just a filename, without the path. So your tests will fail unless your current directory is the del_dir. You need to do a os.path.join(del_dir, f) to get the full path to pass to your tests.

John Anderson
  • 35,991
  • 4
  • 13
  • 36