5
$\begingroup$

In terminal, I run a python code like this way:

python code.py file.txt

Or it might also work as:

./code.py file.txt

Where the 'python' command is for running the job, 'code.py' is the python script and 'file.txt' is the file on which the code is working.

Now my query is, if I use the same code script in Jupyter notebook (i.e. not in the terminal, but from the laptop installed software), how should I execute the run in Jupyter notebook, as it requires the 'file.txt' file as well?

$\endgroup$

1 Answer 1

7
$\begingroup$

From jupyter-notebook, you can do:

!python script.py

I hope this helps.

$\endgroup$
3
  • $\begingroup$ Thanks @danbaki. It helps. (just the .txt file also needs to be written at the end) $\endgroup$
    – Sak
    Commented Dec 14, 2022 at 8:14
  • 2
    $\begingroup$ @Sak Naturally. All arguments you'd normally pass to the file, still go after the file name. All that changes is a ! is put in front of python. $\endgroup$
    – Mast
    Commented Dec 14, 2022 at 14:51
  • 4
    $\begingroup$ %run script.py {args} used to work too. I haven't tested that lately. You can even run a notebook from a notebook, with %run ./secondarynotebook.ipynb $\endgroup$
    – Mast
    Commented Dec 14, 2022 at 14:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .