How to check, change current working directory and list its files in Jupyter notebook

To display handy commands and magics for working directory operation and to display its files in the Jupyter notebook

In the Jupyter Notebook, there are magic commands used to check the working directory, display the contents in the working directory, create a new working directory, as well as change the working directory.

1. Check Current Working Directly

(1) %pwd magic

As its name shows, %pwdis used to print working directory.

%pwd

The output on Windows looks something like: '’G:\\windows\\practical_jupyter\\jupyter_workspace’'

(2) !pwd on Linux

There is an alternative command is !pwd on Linux.

!pwd

But !pwd is not recognized as an internal or external command, operable program or batch file on Windows. Besides, the difference between % and ! are:

% is provided by the IPython kernel and allows you to run “magic commands”

!is provided by Jupyter, allows shell commands to be run within cells.

2. List the Files in Working Directory

(1) %ls

As it names suggests, %ls is used to list all the files in the current working directory.

%ls

The output looks something as follows on Windows.

Volume in drive G is Seagate Backup Plus Drive
Volume Serial Number is EC27-9515

Directory of G:\windows\practical_jupyter\jupyter_workspace

2022-11-02 08:25 PM <DIR> .
2022-10-29 09:44 PM <DIR> ..
2021-11-05 08:53 PM 165 .~curr_intr.pptx
2022-11-02 01:44 PM <DIR> .ipynb_checkpoints
2022-01-30 12:15 AM <DIR> __pycache__
2021-10-10 09:10 PM 19,645 10.create_table.ipynb
2022-10-29 04:04 PM 38,133 11.Insert_equation.ipynb
2022-10-27 07:51 PM 27,812 12.embed_image.ipynb
2022-11-02 06:24 AM 643,332 13.embed_audio_video.ipynb
2021-10-26 03:40 PM 26,338 14.2r_jupyter.ipynb
2021-11-05 10:40 PM 27,115 14.runotherlanguages.ipynb
2021-11-06 08:30 AM 7,141 15.change_theme.ipynb
2021-10-27 10:47 AM 6,463 16.shortcuts.ipynb
2021-10-27 03:48 PM 5,357 17.multiline_cursor.ipynb
2021-11-06 08:39 AM 3,129 18.auto_completion.ipynb
2022-11-02 08:25 PM 28,545 19.magic_commands.ipynb
2022-10-24 07:57 PM 4,510 20.builtin_terminal_unvaliables.ipynb
2021-10-28 02:09 PM 412,439 21.basic_plot.ipynb
2021-11-06 08:50 AM 4,322,344 22.interactive_plot.ipynb
2021-11-06 08:54 AM 17,051 23.interactive_plot_widget.ipynb
2022-11-02 08:00 AM 7,706 24.content_table.ipynb
2022-11-02 03:00 PM 7,533 24.content_table-Copy1.ipynb
2022-10-27 07:53 PM 16,834 25.2.simple_slidedeck.ipynb
2021-10-29 02:43 PM 13,784 25.notebook_slideshow.ipynb
2021-11-06 09:07 AM 124,797 26.web_ applications.ipynb
2022-10-12 11:17 AM 11,536 7.python_programm.ipynb
2022-10-30 09:43 AM 14,534 8.markdown.ipynb
2022-10-30 09:51 AM 18,255 9.html_markdown.ipynb
2022-01-30 12:15 AM <DIR> audio_videos
2022-01-30 12:15 AM <DIR> bg_musics
2021-10-31 02:11 PM 1,746 course_address.txt
2021-11-09 11:47 AM 12,674 course_curriculum_intr.docx
2021-11-08 12:22 PM 31,430,089 course_intr.mp4
2021-11-07 09:28 PM 10,238,779 course_intr.pptx
2021-11-08 01:40 PM 394,063,816 course_intr_music.mp4
2022-09-10 04:19 PM <DIR> course-image
2021-11-09 11:35 AM 90,896,753 Curriculum_intr.pptx
2022-01-30 12:15 AM <DIR> data
2021-09-21 11:46 AM 718 firstnotebook.ipynb
2022-01-30 12:15 AM <DIR> gif_images
2021-09-23 01:41 PM 64 greeting.py
2022-10-12 10:29 AM 46 hello.py
2021-09-23 01:36 PM 38 hi.py
2022-01-30 12:15 AM <DIR> images
2021-10-30 09:46 PM 290,197 jupyter_course_image.png
2021-11-01 08:58 AM 1,855,845 jupyter_header.png
2022-01-30 12:15 AM <DIR> myfolder
2021-09-23 01:37 PM 652 myname.ipynb
2022-01-30 12:15 AM <DIR> notebook_linkages
2022-01-30 12:15 AM <DIR> plots
2021-10-19 12:53 PM 12,551 R_python.ipynb
2021-11-09 08:17 AM 249,476 Run and learn the notebook on cloud environment wi.docx
2021-11-09 08:17 AM 297,541 Run and learn the notebook on cloud environment wi.pdf
2021-11-04 04:30 PM 3,811 simple_slidedeck.ipynb
2021-10-29 02:40 PM 583,330 simple_slidedeck.slides.html
2021-10-30 12:29 PM 1,489 simple3DSurfPlot.ipynb
2021-10-27 09:45 PM 450 simplePiePlot.py
2021-11-07 11:03 AM 2,804 sinplotApp.ipynb
2021-10-29 07:05 AM 3,616 sinplotApp-Copy1.ipynb
2021-11-03 10:42 PM 1,364 Text.txt
2021-10-30 10:10 AM 22,993 Untitled.ipynb
2022-10-29 04:46 PM 1,651 Untitled1.ipynb
2021-11-04 09:10 AM 1,170 Untitled2.ipynb
2022-10-30 10:21 AM 1,054 Untitled3.ipynb
51 File(s) 535,779,215 bytes
13 Dir(s) 1,045,533,704,192 bytes free

If you use Linux, the output maybe look a little different.

(2) !ls

Besides, for Linux, we can also use:

!ls

However, !ls is not recognized as an internal or external command, operable program or batch file on Windows.

(3) List one type of file

Just display one type of file, for example, we want to look for .py file, we can do as follows:

%ls *.py

The result looks as:

Volume in drive G is Seagate Backup Plus Drive
Volume Serial Number is EC27-9515

Directory of G:\windows\practical_jupyter\jupyter_workspace

2021-09-23 01:41 PM 64 greeting.py
2022-10-12 10:29 AM 46 hello.py
2021-09-23 01:36 PM 38 hi.py
2021-10-27 09:45 PM 450 simplePiePlot.py
4 File(s) 598 bytes
0 Dir(s) 1,045,533,696,000 bytes free

Besides, we can use !ls *.py alternative on Linux, but it does not work on Windows.

3. Change Working Directory

(1) %cd

As its name suggests, the %cd magic is used to change your working directory.

On Linux:

It uses slash /in the path.

%cd /home/sigmund/Documents

On Windows:

It can use backslash\ or double backslash\\.

%cd D:\workspace

or

%cd D:\\workspace

(2) !cd

!cdworks on both Linux and Windows. For example, on Window, we can type:

!cd D:\\workspace

4. Video Tutorial

If you want to see more examples, you are welcome to watch the video tutorial on my YouTube channel. If this video is helpful, please subscribe to my channel to support me.

5. Online Course

If you are interested in learning Jupyter notebook in details, you are welcome to enrol one of my course Practical Jupyter Notebook from Beginner to Expert.

Bookmark
ClosePlease loginn
0 - 0

Thank You For Your Vote!

Sorry You have Already Voted!

Please follow and like me:

Leave a Reply

Your email address will not be published. Required fields are marked *