Python Forum
deleting files in program files directory
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
deleting files in program files directory
#5
Also a advice use pathlib
Example.
from pathlib import Path, PurePath
import os

folder_path = r'C:\Program Files (x86)\Microsoft'
for file_path in Path(folder_path).rglob('*'):
        if file_path.is_file():
            #print(file_path)
            if 'url_sample.csv' in file_path.parts:
                print(f'File <{file_path}> is deleted')
                os.remove(file_path)
Output:
G:\div_code\reader_env λ python del_file.py File <C:\Program Files (x86)\Microsoft\EdgeUpdate\Download\url_sample.csv> is deleted Traceback (most recent call last): File "G:\div_code\reader_env\del_file.py", line 10, in <module> os.remove(file_path) PermissionError: [WinError 5] Ingen tilgang: 'C:\\Program Files (x86)\\Microsoft\\EdgeUpdate\\Download\\url_sample.csv
As mention running without administrative privileges will not work with these system folders.
Here run shell that i use cmder with administrative privileges,then it works.
Output:
G:\div_code\reader_env λ python del_file.py File <C:\Program Files (x86)\Microsoft\EdgeUpdate\Download\url_sample.csv> is deleted
Reply


Messages In This Thread
RE: deleting files in program files directory - by snippsat - Aug-19-2024, 08:04 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Correct/proper way to create save files snakes 0 438 Mar-11-2025, 06:58 PM
Last Post: snakes
  Merge htm files with shutil library (TypeError: 'module' object is not callable) Melcu54 7 3,299 Mar-09-2025, 04:25 PM
Last Post: Pedroski55
  how to download large files faster? kucingkembar 3 776 Feb-20-2025, 06:57 PM
Last Post: snippsat
  Inserting Python Buttons into KV Files edand19941 3 474 Feb-19-2025, 07:44 PM
Last Post: buran
Question [SOLVED] Right way to open files with different encodings? Winfried 3 3,965 Jan-18-2025, 02:19 PM
Last Post: Winfried
  Applications config files / Best practices aecordoba 2 1,854 Oct-23-2024, 12:56 PM
Last Post: aecordoba
  Compare 2 files for duplicates and save the differences cubangt 2 926 Sep-12-2024, 03:55 PM
Last Post: cubangt
  Convert Xls files into Csv in on premises sharpoint Andrew_andy9642 3 984 Aug-30-2024, 06:41 PM
Last Post: deanhystad
  I'm trying to merge 2 .csv files with no joy! Sick_Stigma 3 914 Aug-03-2024, 03:20 PM
Last Post: mariadsouza362
  Class test : good way to split methods into several files paul18fr 5 3,534 Jul-17-2024, 11:12 AM
Last Post: felixandrea

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020