Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Explain how to delete a file in Python?

2 Answers

  • Lv 7
    1 month ago

    you need to have 2 things:

    1. the path of the file's location, either relative to the running file, or an absolute path, which may differ, depending on the OS.

    2. the filename of the file in question, including any extension.

  • oyubir
    Lv 6
    1 month ago

    import os


    The same function also goes by the name os.remove(...).

    It is just 2 names for the exact same thing. "remove" is probably easier to memorize for you, since you (improperly, I am afraid) asked how to "delete" a file (don't worry. I say "delete" or "remove" all the time also. But strictly speaking, nobody delete a file).

    Because what those function do (like the "del" command in msdos, or "rm" command in unix, and "rm" stand obviously for "remove") is to suppress the NAME of the file. Not the file itself.

    The file will be suppressed by the underlying filesystem when it has no name left.

    Most of the files only have one name.

    But if several hard link exist on the file, then, it is only when the last reference is removed that the file is deleted.

    Most of the time, you don't care, and you can treat my whole answer (but the 2 first lines) as an old prof rant.

    But if what you want to do is really remove the file, so that nobody can ever see the content, neither os.remove nor os.unlink will no (nor would commands like "rm", "del", or suppressing the file in a file explorer. You need specific tools do to that).


    import os


Still have questions? Get your answers by asking now.