How to recover unsaved file in spyder - python

Spyder crashed at the last minute and I had force quit the programme as it wasn't responding at all. After reopening it, I am not able to find my file that I was working on, i hadn't saved it and had quite a bit of code written in it. It will be a huge help if someone knows a way to recover it, and can guide me through it.
Thanks alot!

hist -g will show console history logs. You can recover your code from history if you had run your code earlier.

I experienced a similar situation where I had added code to existing script that later wasn't saved as I expected it to be. I was able to recover my code from the history log (copy and paste). Not a perfect solution, but much better than having to re-write the code.
I am on Spyder 3.3.4

I'll comment to help in case (someone crazy like me) clicked the discard button when Spyder asked if you wanted to restore the file.
I found the following solution:
open a new terminal
cd /home/pc/.config/spyder-py3/
code .
open the history.py file
Heart attack stopped successfully!
Anyway, you will no longer find the file you discarded in the autosave folder, however, in the history.py file are saved the last 1000 lines that you executed, in my case the last 30 were enough.
I hope it helps

Just for the people who still want to know the solution, close and open again Spyder application. It will ask you to recover the file. This worked for me today.

Go to C:\path.spyder-py3
You will have directories with the name autosave, you can find your autosaved files here. These are same ones that come up when you close and open spyder. It loads it from this autosave dir.
Another method is to check the history.py file which stores commands run in spyder console. However, there is a limit to this history. The old ones will get deleted.

Related

ModuleNotFoundError in python in cs231 course

I have decided to complete cs231 course and do its assignment. I happily watched the first 2 videos of the course and now I had to solve the first assignments.
I followed the guidelines step by step which was shown in the video in this link:
https://cs231n.github.io/setup-instructions/
Then, when I run the first cell, which is not the cell shown in the video but nonetheless it's in the assignments1 file which I downloaded from their site, I get a nasty error which has paralyzed me four a couple of hours. I'd be happy if anyone could respond.
IF you take a look at my picture, you'll see that files are added in the google drive, but surprisingly, it gives an error out of nowhere.
Thanks.
===========================================================================
Update:
Here is the snapshot of the video provided to guide students how to setup their google colab (in that link).
As you can see, in their vide the first chunk of code specifies their working directory but in the file that they have uploaded as their assignment1, they have not done so!
cs231n is a virtual environment according to documentation from the link u provided.
Every time you want to work on assignment you should activate that environment by source ~/cs231n/bin/activate
This happens because cs231n is not in the current path. Add these line at the beginning of the code :
import sys
sys.path.append('./cs231n')
I had the same problem. I ran the first block which imports the module 'sys' and appends the file path. After that I left and came back after 5 hours. When I log in again, I didn't run the first block. Then I came with the problem. That's the question! The new Google Drive allocated is different. Therefore, the python doesn't know where the file path is. So I tried to run the first block again and it really works. That's why you succeed by downloading the assignment1 file and running it again.
This is my opinion. Hope this can help you!
This was ridiculous!
After dealing with it for couple of hours (trying to discover the current directory using cwd command and cd) with no avail, I decided to clear ALL files from my google drive and download the assignment1 file again
Suprisingly, this time the colab page had the code in its beginning. I still wonder how that happened.

Can't write to file, even though I have permissions to do so In Python

As the tittle implies, i am having trouble writing to a certain file, even though I have permissions to do so. I'm not getting an error; It's like the lines of code are just being ignored. They're not doing anything.
f = open("C:/ProgramData/Microsoft/Windows/Start Menu/Programs/StartUp/startupfile.py", 'w')
f.write("Stuff I want to write")
f.close()
Can anyone help me? Again, there's no error message. The lines are just being flat-out ignored. And yes, I am running it with permissions through the console. Is the problem that it's in the windows directory? Or am I just missing something painfully obvious here?
EDIT:
Thanks for the help everyone, but it turns out that the problem was just Windows being incompetent. I found that the files appeared after a restart, which works anyway because I'm writing in the Startup folder.
I don't have enough reputation to make a simple comment, so don't interpret this as a solution to your problem, but did you try opening the file in the "wt" mode instead of only "w"? "w" mode opens the file in binary as seen here, so the issue could be that the program doesn't understand what you are trying to say because you want to edit it in the text mode. Again, I'm not sure if this is right and I'm a newbie myself, so don't interpret this as a solution, more like a commentary.

Recovering command history in Jupyter Notebook?

I have been editing a Jupyter Notebook for the past week, and tried saving it today. While attempting to save it, I got an error, so I refreshed the page and successfully saved it.
However, to my dismay almost all of my command history was lost! I still have access to the variables (the kernel never died), but I don't have access to any of the code.
Is there any way to recover the code? The kernel is still running, but I do not see any checkpoints in my notebook.
You can get the IPython history, which Jupyter Notebook uses as its kernel. In one of the cells run:
%history
This will dump a history (good, bad, and ugly) of every command you have run in the current IPython session. It is probably more than you want, but it is better than losing all your work.
Something similar happened to me where I could not save what I did, refreshed the page, and lost all my commands.
You can get your entire notebook history and redirect the output to any given file by running
%history -g -f filename
In my case, I redirected it to a (.py) file. You can find more documentation here https://ipython.readthedocs.io/en/stable/interactive/magics.html under "%history".
If your kernel hasn't restarted you can get your code from globals like _i{1,2,3,...}.
The simplest way to find your code is to run something like
print([k for k, v in globals.items() if 'some unique code snippet' in str(v)])
Say you got something like ['_i2', '_i5', '_i9'], then you can
print(_i9)
and copypaste it to a new cell.

How to open a Unix Executable File using Python?

Sorry if this might be an easy question, but I'm trying to open a Unix Executable File using Python, but it doesn't have any file extensions attached to it. The file name looks something like 'filename_bib'. I typed this and it worked:
hdulist = open('filename_bib')
But next when I typed in hdulist.info() or hdulist.shape(), it doesn't give me anything, so I checked all its attributes and tried print(type()) and hdulist.attribute? for each attribute, but I didn't really understand any of the explanations, so I actually tried typing all of them to see what they would give me, but at some point it started giving me errors which said:
ValueError: I/O operation on closed file
so I think this may have happened when I tried using hdulist.close() or hdulist.closed(), but I don't know (1) if it was a mistake for me to try any of the attributes, (2) if it somehow changed anything from my original file, and (3) how to fix it.
I was told that this file contains bytes and that I should somehow be able to show a picture from it using Python, but this is my first time handling Unix Executable Files, and I have absolutely no idea how to start. I've handled fits and pl files before, but this is my first time trying to open something like this. I've tried looking up a bunch of things online already, but I can't find any instructions whatsoever. Please help me out if you know anything about this. I will be very grateful for any help that you could give me.
This is what it shows when I open it in Sublime:
enter image description here
As the default file access mode in python is "read only". Technically, since you have not mentioned any access mode in your command
hdulist = open('filename_bib')
file should only be for reading and nothing should have happend to the opened file.
Question:
Have you tried running it in UNIX by,
./filename_bib
What was the output?

Back end process in windows

I need to run the python program in the backend. To the script I have given one input file and the code is processing that file and creating new output file. Now if I change the input file content I don't want to run the code again. It should run in the back end continously and generate the output file. Please if someone knows the answer for this let me know.
thank you
Basically, you have to set up a so-called FileWatcher, i.e. some mechanism which looks out for changes in a file.
There are several techniques for watching file/directory changes in python. Have a look at this question: Monitoring contents of files/directories?. Another link is here, this is about directory changes but file changes are handled in a similar way. You could also google for "watch file changes python" in order to get a lot of answers :)
Note: If you're programming in windows, you should probably implement your program as windows service, look here for how to do that.

Categories

Resources