pythonpython-3.xjupyterjupyter-irkernel

Error %%writefile not found when try to modified txt file


I got this error message:

UsageError: Line magic function %%writefile not found.

Please check my code and service log from jupyter notebook. Please help me.\

Version : Python 3.11.3 Environemt : Code-Server Docker Container (Ubuntu 20.04 Jammy).

enter image description here enter image description here

Thanks.

I already tried to restart the kernel, re-run Jupyter Notebook, and re-install Ipykernel. But all of that doesn't make any difference. Still, I'm not able to try to modify my text file.


Solution

  • The Cell magic fuction i.e %% must be on the first line where you have the comment. You don't have it on top so it was parsed as Line magic function %%writefile not found. i.e. %.

    works:

    Cell magic fuction on first line.

    %%writefile file.txt
    hello
    

    or Cell magic fuction on second line where the first line is empty.

    
    %%writefile file.txt
    hello
    
    doesn't work:

    comments or any other line on top

    # some thing
    %%writefile file.txt
    hello
    
    for your case:

    removing comment works fine

    %%writefile ../notes/perform-10.txt
    
    test