Jupyter Notebook : 'head' is not recognized as an internal or external command, operable program or batch file

Viewed 7079

I tried to view my CSV file using

!head {train_file_path} 

in jupyter notebook.But it raises an error

'head' is not recognized as an internal or external command,
operable program or batch file.

!head{} works in colab but not in Jupyter Notebook.

please help me, guys . Thanks in advance

6 Answers

Using ! means that you will be calling a system command. If you are on a Linux/Unix system (Google Colab uses such system) then you can call Linux/Unix commands directly using !. In this case, I am assuming that you are using a Windows system and the command head does not exist as a command for Windows. Assuming that you are using a locally hosted Jupyter Notebook, then it is running on a Windows system.

You can do something similar through Python using:

with open({train_file_path}) as f:
    for _ in range(10): # first 10 lines
        print(f.readline())

I had the same issue even if I installed the Linux Subsystem for Windows and tried to refer Jupyter Lab to bash.exe. This seems to work for terminal sessions in Jupyter Lab, but not in the notebook cells.

A workaround for me was to add a %%bash at the beginning of each cell with bash commands. Then also remove the ! from the actual commands, like so

%%bash
head iris.csv

Still cannot make it work with an ! directly, but at least it works.

I am using Anaconda in windows: For users like me following steps are recommended:

1.Go to your Anaconda Navigator 2. Inside environments click on the play button of base(root) and select open terminal 3. This is your conda environment terminal 4. Type:

conda install posix
  1. after the installtion is finished go back to your jupyter notebook or lab and run the !head code again. It should work now.

See this answer.

You can replace cmd with powershell and use the !gc yourfile.txt -TotalCount 10 command.

Try this

%alias head powershell -command "& {Get-Content %s -Head 5}"

%head datasets/transfusion.data
Related