I have a python file named initialization.py. When trying to copy-paste contents into a blank file, I used ctrl + x to copy/cut, closed the original file, and accidentally used ctrl + x again in the blank document of my text editor, then proceeded to save the blank document with the same filename initialization.py. So when I use ctrl + v, I am pasting empty space rather than the code. Is there a way to retrieve the contents of the python file before it was overwritten?

In case it's relevant, I'm using macOS High Sierra 10.13.6 and my text editor is Atom 1.37.0.


I followed the steps posted in this answer, but now I'm not sure what to do next.

terminal screen-capture

  • 1
    Restore the file from Time Machine backup? – Kusalananda May 29 at 11:05
  • I tried. The feature has to be turned on to work, mine was turned off. When opening the menu, the up/down arrows are not clickable. – allthemikeysaretaken May 29 at 11:06
  • 1
    So you did cut, save and close the original file? So no chance to undo? And the linked answer doesn't help you, because the file was not deleted, but the contents were overwritten. Different thing. With APFS version history, the data would have been saved to a different block, so you could have restored it. And you also don't have a clipboard extension? With this combination of suicidal actions I don't see a chance to recover your contents. – Philippos May 29 at 13:41
  • A great lesson here is use git and make regular "micro" commits. You can always rebase or squash merge them together. – Philip Couling May 29 at 14:40

Mistakes like this is why I wrote a script to automatically backup all of the small text files on my machine every 10 minutes. (also useful if you want to view the previous version of a file)

What you're asking is a data recovery question now:

  1. Do NOT use the drive for anything else. If it's your main system drive, turn the computer off immediately.

  2. You can try using the TestDisk and the PhotoRec tool for Mac to recover your files. It will produce thousands of different files, all unlabelled.

  3. Search through these files for a unique text string you remember from the script. Hopefully it's still there. Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.