Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change current working directory in IPython (Windows)

I'm somehow unable to change the current working directory in IPython and I have no idea why.

In the example below I start IPython from the root of the C drive and try to change the current working directory in several ways. Weird things result:

C:\>ipython
Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)]
Type "copyright", "credits" or "license" for more information.

IPython 2.0.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: cd
C:\Users\jkokorian

In [2]: %cd "C:\Dell"
C:\Dell

In [3]: cd
C:\Users\jkokorian

In [4]: import os

In [5]: os.chdir("C:\Dell")

In [6]: os.getcwd()
Out[6]: 'C:\\Dell'

In [7]: cd
C:\Users\jkokorian

In [8]: os.getcwd()
Out[8]: 'C:\\Users\\jkokorian'

Somehow the working directory always defaults to my home folder, even when IPython is started from the root of C:.

Does someone have a clue what's going on here?

like image 794
jkokorian Avatar asked May 19 '14 09:05

jkokorian


2 Answers

After some experimenting I figured out that the 'cd' magic command without any arguments resets the current working directory to 'C:\Users\jkokorian'. I assumed that it would echo the current working directory, but apparently it doesn't.

like image 152
jkokorian Avatar answered Oct 18 '22 23:10

jkokorian


If you wish to run certain .py file under certain directory which differs from the current directory. You need execute the following 2 rows together:

%cd C:\example_folder1\example_folder2\
%run example.py

mind that the last \ can make sure it returns to next row instead of execute the current row.

like image 39
S.Gu Avatar answered Oct 18 '22 23:10

S.Gu