jwm
jwm

Reputation: 5040

In python, how to restore the function of os.chdir() if os.chdir = 'path' has been implemented

In path setup, I wrongly wrote the code: os.chdir = '\some path', which turns the function os.chdir() into a string. Is there any quick way to restore the function without restarting the software? Thanks!

Upvotes: 8

Views: 3263

Answers (2)

boot-scootin
boot-scootin

Reputation: 12515

>>> import os

Assign to the chdir method a string value:

>>> os.chdir = '\some path'
>>> os.chdir
'\some path'

Use reload to, well, reload the module. reload will reload a previously imported module.

>>> reload(os)
>>> os.chdir
<built-in function chdir>

Upvotes: 2

wim
wim

Reputation: 363183

Kicking os out of the modules cache can make it freshly importable again:

>>> import sys, os
>>> os.chdir = "d'oh!"
>>> os.chdir()
TypeError: 'str' object is not callable
>>> del sys.modules['os']
>>> import os
>>> os.chdir
<function posix.chdir>

Upvotes: 8

Related Questions