Change the current working directory. This command automatically maintains an internal list of directories you visit during your IPython session, in the variable _dh. The command %dhist shows this history nicely formatted. You can also do 'cd - ' to see directory
(self, parameter_s='')
| 299 | @skip_doctest |
| 300 | @line_magic |
| 301 | def cd(self, parameter_s=''): |
| 302 | """Change the current working directory. |
| 303 | |
| 304 | This command automatically maintains an internal list of directories |
| 305 | you visit during your IPython session, in the variable _dh. The |
| 306 | command %dhist shows this history nicely formatted. You can also |
| 307 | do 'cd -<tab>' to see directory history conveniently. |
| 308 | |
| 309 | Usage: |
| 310 | |
| 311 | cd 'dir': changes to directory 'dir'. |
| 312 | |
| 313 | cd -: changes to the last visited directory. |
| 314 | |
| 315 | cd -<n>: changes to the n-th directory in the directory history. |
| 316 | |
| 317 | cd --foo: change to directory that matches 'foo' in history |
| 318 | |
| 319 | cd -b <bookmark_name>: jump to a bookmark set by %bookmark |
| 320 | (note: cd <bookmark_name> is enough if there is no |
| 321 | directory <bookmark_name>, but a bookmark with the name exists.) |
| 322 | 'cd -b <tab>' allows you to tab-complete bookmark names. |
| 323 | |
| 324 | Options: |
| 325 | |
| 326 | -q: quiet. Do not print the working directory after the cd command is |
| 327 | executed. By default IPython's cd command does print this directory, |
| 328 | since the default prompts do not display path information. |
| 329 | |
| 330 | Note that !cd doesn't work for this purpose because the shell where |
| 331 | !command runs is immediately discarded after executing 'command'. |
| 332 | |
| 333 | Examples |
| 334 | -------- |
| 335 | :: |
| 336 | |
| 337 | In [10]: cd parent/child |
| 338 | /home/tsuser/parent/child |
| 339 | """ |
| 340 | |
| 341 | try: |
| 342 | oldcwd = os.getcwd() |
| 343 | except FileNotFoundError: |
| 344 | # Happens if the CWD has been deleted. |
| 345 | oldcwd = None |
| 346 | |
| 347 | numcd = re.match(r'(-)(\d+)$',parameter_s) |
| 348 | # jump in directory history by number |
| 349 | if numcd: |
| 350 | nn = int(numcd.group(2)) |
| 351 | try: |
| 352 | ps = self.shell.user_ns['_dh'][nn] |
| 353 | except IndexError: |
| 354 | print('The requested directory does not exist in history.') |
| 355 | return |
| 356 | else: |
| 357 | opts = {} |
| 358 | elif parameter_s.startswith('--'): |