I want to create a file; if it already exists I want to delete it and create it anew. I tried doing it like this but it throws a Win32 error. What am I doing wrong?
Windows won't let you delete an open file (unless it's opened with unusual sharing options). You'll need to close it before deleting it:
try: with open(os.path.expanduser('~') + '\Desktop\input.txt') as existing_file: existing_file.close() os.remove(os.path.expanduser('~') + '\Desktop\input.txt')