Why does %cat work on Unix, but not Windows?

I teach a lot of Python courses using Jupyter, and as a Mac user, show them the contents of files using the %cat magic command. It would seem that %cat doesn’t exist or work on Windows. Now, I know that you could argue cat is a Unix command and thus doesn’t exist on Windows, but is there a reason this doesn’t work, when such Unixisms as %ls do work?

Where do the %cat and %ls magics come from in the first place? I don’t see them listed among the IPython magics: https://ipython.readthedocs.io/en/stable/interactive/magics.html

There’s an SO answer that suggests defining %cat as an alias: https://stackoverflow.com/a/32675775/5629418
That alias would then behave as if you had typed !cat, and ! is the magic for executing shell commands, which would explain the difference.

Aha! It never dawned on me that when you write %cat, Jupyter translates it and runs !cat. But that makes a heckuva lot of sense, and explains the difference.

I’m not about to ask my students to start with aliases, so I’ll see if there’s another good way to handle the show-a-file situation that’s cross platform.

I see there’s %pycat https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-pycat

But we have the same use case / feature request to have a %cat (or new command with that function) that also works on Windows, so that we can shot the file contents or TXT, YAML, … files from tutorial notebooks in a cross-platform way.

Does a workaround / current solution exist?
Is print(pathlib.Path("file.txt").read_txt()) the simplest way?

Are devs looking at this forum? Would chances to get this increase if a feature request in the IPython tracker were filed?

1 Like