Reputation: 3095
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?
Upvotes: 12
Views: 38591
Reputation: 649
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.
Upvotes: 6
Reputation: 3095
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.
Upvotes: 24