How To Write Text To File In Ipython And Jupyter Notebook

IPython provide a lot of magic commands, with %%writefile command, you can create text file and write text to it in ipython interface and jupyter notebook easily. This article will show you two examples to demo how to use it.

1. Write Text To File In Ipython.

  1. Open a terminal and input ipython command.
  2. Then write below source code in the ipython interactive console. You can input multiple line text, to end input text and write text to file click Esc key and then click Enter key. Then ipython will write all your input line text into the file. If the target file exist, then the file content will be overrided.
    $ ipython
    Python 3.7.1 (default, Dec 14 2018, 19:28:38) 
    Type 'copyright', 'credits' or 'license' for more information
    IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help.
    
    In [1]: %%writefile abc.txt 
       ...: hello ipython, this is the first text file. 
       ...:                                                                                                                                                                                                     
    Writing abc.txt
    
    In [2]: %%writefile abc.txt 
       ...: hello ipython world 
       ...: this is from command line 
       ...: i love ipython 
       ...:                                                                                                                                                                                                     
    Overwriting abc.txt
    
  3. Run below command to check the text file content.
    In [3]: ls -l  # this command make sure abc.txt file has been created.                                                                                                                                                                                           
    total 72
    -rw-r--r--  1 zhaosong zhaosong   61 3月  19 10:01 abc.txt
    drwxr-xr-x 24 zhaosong zhaosong 4096 3月   3 20:37 anaconda3/
    drwxr-xr-x  3 zhaosong zhaosong 4096 12月  2 20:13 Android/
    drwxr-xr-x  2 zhaosong zhaosong 4096 12月 20 20:38 Desktop/
    drwxr-xr-x  3 zhaosong zhaosong 4096 11月 16 14:31 Documents/
    drwxr-xr-x  3 zhaosong zhaosong 4096 3月   7 10:07 Downloads/
    drwxrwxr-x  3 zhaosong zhaosong 4096 12月 16 20:40 eclipse/
    drwxrwxr-x  3 zhaosong zhaosong 4096 12月 16 20:49 eclipse-workspace/
    -rw-r--r--  1 zhaosong zhaosong 8980 11月 16 12:08 examples.desktop
    drwxr-xr-x  2 zhaosong zhaosong 4096 11月 16 12:30 Music/
    drwxr-xr-x  2 zhaosong zhaosong 4096 3月  18 21:17 Pictures/
    drwxr-xr-x  2 zhaosong zhaosong 4096 11月 16 12:30 Public/
    drwxr-xr-x  3 zhaosong zhaosong 4096 1月  21 12:50 snap/
    drwxr-xr-x  2 zhaosong zhaosong 4096 11月 16 12:30 Templates/
    drwxr-xr-x  2 zhaosong zhaosong 4096 3月   4 10:44 Videos/
    drwxr-xr-x  4 zhaosong zhaosong 4096 1月   8 11:57 WorkSpace/
    
    # open abc.txt with read permission.
    In [4]: file = open('./abc.txt','r')                                                                                                                                                                        
    
    # print abc.txt file content
    In [5]: print(file.read())                                                                                                                                                                                  
    hello ipython world
    this is from command line
    i love ipython
    
  4. Run exit() function to exit ipython interface.
    In [6]: exit()                                                                                                                                                                                              
    [email protected]:~$

2. Write Text To File In Jupyter Notebook.

  1. Run $ jupyter notebook command to start jupyter web server.
    $ jupyter notebook
    [I 10:10:48.377 NotebookApp] The port 8888 is already in use, trying another port.
    [I 10:10:48.606 NotebookApp] JupyterLab extension loaded from /home/zhaosong/anaconda3/lib/python3.7/site-packages/jupyterlab
    [I 10:10:48.606 NotebookApp] JupyterLab application directory is /home/zhaosong/anaconda3/share/jupyter/lab
    [I 10:10:48.607 NotebookApp] Serving notebooks from local directory: /home/zhaosong
    [I 10:10:48.607 NotebookApp] The Jupyter Notebook is running at:
    [I 10:10:48.607 NotebookApp] http://localhost:8889/?token=5ac4d4ff2c59bafd77f5c82bdf4c56199497792f499b8e29
    [I 10:10:48.607 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
    [C 10:10:48.703 NotebookApp] 
        
        To access the notebook, open this file in a browser:
            file:///run/user/1000/jupyter/nbserver-8327-open.html
        Or copy and paste one of these URLs:
            http://localhost:8889/?token=5ac4d4ff2c59bafd77f5c82bdf4c56199497792f499b8e29
    [W 10:11:02.460 NotebookApp] delete /abc.txt
    
  2. Click the url to open jupyter notebook web gui interface. The url should contain the token value.
  3. Click New —> Python 3 menu item to create a new jupyter notebook file (.ipynb file).run python 2 and python 3 code in jupyter notebook web server
  4. Input below ipython code in line cell one, then click the Run button to run it to create file abc.txt and write text data to it.
    %%writefile abc.txt
    hello world
    i love ipython
    jupyter notebook is so wonderful
  5. Input below ipython code in line cell two, click Run button to read abc.txt file content out.
    with open('abc.txt', 'r') as f:
        print(f.read())
  6. Run below code in line cell three to list the created abc.txt file.
    ls -l
  7. Below is the web page picture about above actions.
    jupyter notebook write text to file web ui
  8. Let us look at the terminal console, you can see below running code output of above process.
    Creating new notebook in 
    [I 10:14:24.947 NotebookApp] Kernel started: 767dc231-42a9-470f-8a07-f8c9e5621e9f
    [I 10:14:25.620 NotebookApp] Adapting to protocol v5.1 for kernel 767dc231-42a9-470f-8a07-f8c9e5621e9f
    [I 10:16:25.339 NotebookApp] Saving file at /Untitled.ipynb
    [I 10:18:36.869 NotebookApp] 302 GET /?token=5ac4d4ff2c59bafd77f5c82bdf4c56199497792f499b8e29 (127.0.0.1) 0.51ms
    [I 10:18:40.677 NotebookApp] Adapting to protocol v5.1 for kernel 767dc231-42a9-470f-8a07-f8c9e5621e9f
    [I 10:20:40.646 NotebookApp] Saving file at /Untitled.ipynb
    [I 10:22:40.651 NotebookApp] Saving file at /Untitled.ipynb
READ :   How To Fix PyDev Invalid External Source Folder Specified Error

1 Comment

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.