Last active
March 2, 2022 13:48
-
-
Save orbyfied/2993954cec255d4cb50c6ff04d2441d8 to your computer and use it in GitHub Desktop.
Erases a specified file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import os.path | |
# get file to erase | |
fn = input("file to erase: ") | |
# open file' | |
try: | |
# check if it exists | |
if not os.path.exists(fn): | |
raise IOError("file " + fn + " does not exist") | |
# confirmation | |
confirm = input("type y to confirm erasure of file. this is irreversible: ") | |
if (confirm != 'y'): | |
sys.exit(0) | |
# open stream, write and close | |
f = open(fn, 'w+') | |
f.write("") | |
f.close() | |
except OSError as e: | |
print("error while opening file or writing to file: " + str(e)) | |
sys.exit(1) | |
# done | |
print("successfully erased file " + fn) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment