Repository: spyder-ide/spyder

Issue:
Issues with recognizing directory renaming
## Description

**What steps will reproduce the problem?**

1.Probably a weird corner case that involves me doing things that are very contrary to best practices.  Renaming directories on the fly and then trying to move forward / backward in the File Explorer history caused the error.  Also noticed issues with properly refreshing directory contents, etc.

**What is the expected output? What do you see instead?**


**Please provide any additional information below**

  File "/home/immersinn/.virtualenvs/networks/lib/python3.5/site-packages/spyder/widgets/explorer.py", line 1132, in chdir
    os.chdir(directory)
FileNotFoundError: [Errno 2] No such file or directory: '/home/immersinn/gits/networks/learning-path'

Traceback (most recent call last):
  File "/home/immersinn/.virtualenvs/networks/lib/python3.5/site-packages/spyder/widgets/explorer.py", line 1097, in go_to_previous_directory
    self.chdir(browsing_history=True)
  File "/home/immersinn/.virtualenvs/networks/lib/python3.5/site-packages/spyder/widgets/explorer.py", line 1135, in chdir
    except PermissionError:
UnboundLocalError: local variable 'PermissionError' referenced before assignment


## Version and main components

* Spyder Version: 3.2.2 
* Python Version: 3.5.2
* Qt Versions: 5.9.1, PyQt5 5.9 on Linux

## Dependencies
```
pyflakes >=0.6.0 :  1.6.0 (OK)
pycodestyle >=2.3:  2.3.1 (OK)
pygments >=2.0   :  2.2.0 (OK)
pandas >=0.13.1  :  None (NOK)
numpy >=1.7      :  1.13.1 (OK)
sphinx >=0.6.6   :  1.6.3 (OK)
rope >=0.9.4     :  0.10.7 (OK)
jedi >=0.9.0     :  0.10.2 (OK)
psutil >=0.3     :  5.3.0 (OK)
nbconvert >=4.0  :  5.3.1 (OK)
sympy >=0.7.3    :  None (NOK)
cython >=0.21    :  0.26.1 (OK)
qtconsole >=4.2.0:  4.3.1 (OK)
IPython >=4.0    :  6.1.0 (OK)
pylint >=0.25    :  1.7.2 (OK)

```



Discussion:
Thanks for reporting. We'll take a look at this after we release 3.2.3
@dalthviz, please work on this one.

Resolving patch:
diff --git a/spyder/plugins/workingdirectory.py b/spyder/plugins/workingdirectory.py
--- a/spyder/plugins/workingdirectory.py
+++ b/spyder/plugins/workingdirectory.py
@@ -283,11 +283,13 @@ def chdir(self, directory, browsing_history=False,
             self.histindex = len(self.history)-1
         
         # Changing working directory
-        os.chdir(directory)
+        try:
+            os.chdir(directory)
+            if refresh_explorer:
+                self.set_explorer_cwd.emit(directory)
+            if refresh_console:
+                self.set_current_console_wd.emit(directory)
+            self.refresh_findinfiles.emit()
+        except OSError:
+            self.history.pop(self.histindex)
         self.refresh_plugin()
-
-        if refresh_explorer:
-            self.set_explorer_cwd.emit(directory)
-        if refresh_console:
-            self.set_current_console_wd.emit(directory)
-        self.refresh_findinfiles.emit()
diff --git a/spyder/widgets/explorer.py b/spyder/widgets/explorer.py
--- a/spyder/widgets/explorer.py
+++ b/spyder/widgets/explorer.py
@@ -35,7 +35,7 @@
 # Local imports
 from spyder.config.base import _
 from spyder.py3compat import (getcwd, str_lower, to_binary_string,
-                              to_text_string, PY2)
+                              to_text_string)
 from spyder.utils import icon_manager as ima
 from spyder.utils import encoding, misc, programs, vcs
 from spyder.utils.qthelpers import (add_actions, create_action, file_uri,
@@ -1126,8 +1126,15 @@ def chdir(self, directory=None, browsing_history=False):
                 self.history.append(directory)
             self.histindex = len(self.history)-1
         directory = to_text_string(directory)
-        if PY2:
+        try:
+            PermissionError
+            FileNotFoundError
+        except NameError:
             PermissionError = OSError
+            if os.name == 'nt':
+                FileNotFoundError = WindowsError
+            else:
+                FileNotFoundError = IOError
         try:
             os.chdir(directory)
             self.parent_widget.open_dir.emit(directory)
@@ -1136,6 +1143,9 @@ def chdir(self, directory=None, browsing_history=False):
             QMessageBox.critical(self.parent_widget, "Error",
                                  _("You don't have the right permissions to "
                                    "open this directory"))
+        except FileNotFoundError:
+            # Handle renaming directories on the fly. See issue #5183
+            self.history.pop(self.histindex)
 
 
 class ExplorerWidget(QWidget):


Test patch: