Skip to content

Instantly share code, notes, and snippets.

@orbyfied
Last active March 2, 2022 13:48
Show Gist options
  • Save orbyfied/2993954cec255d4cb50c6ff04d2441d8 to your computer and use it in GitHub Desktop.
Save orbyfied/2993954cec255d4cb50c6ff04d2441d8 to your computer and use it in GitHub Desktop.
Erases a specified file.
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