Aug-19-2024, 08:04 PM
Also a advice use pathlib
Example.
Here run shell that i use cmder with administrative privileges,then it works.
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