[Python] access denied OS module python

Discussão em 'Python' iniciado por Stack, Outubro 6, 2024.

  1. Stack

    Stack Membro Participativo

    Right now I am working on a project a Files manager

    #delete file

    import os

    file_path = r"The\\path_file"

    try:
    os.remove(file_path)
    print("The file has been removed")
    except FileNotFoundError:
    print("File not found.")
    except PermissionError:
    print("Access denied. You don't have permission to remove this file.")
    except Exception as e:
    print(f"An error occurred: {e}")



    I created this code because when I wrote the line it told me I had access denied, I knew it woildn't delete the file I wanted because the file was opened by another program, so i took diferente files, and got always the same problem

    to understand what really was hapening I put a try execpt condition,: try: os.remove(file_path) print("The file has been removed") except FileNotFoundError: print("File not found.") except PermissionError: print("Access denied. You don't have permission to remove this file.") except Exception as e: print(f"An error occurred: {e}")

    and once again it keeps telling me "access denied" I already used VsCode as an admistrator even CMD I keep having the same problem, I searched on stackoverflow but none of the asnwers was working

    Continue reading...

Compartilhe esta Página