databricksdatabricks-community-edition

Databricks notebook: use magic commands for several lines


I may be missing the obvious, but: I am using the Databricks community edition notebook. I am trying to use several %fs lines within the same cell Is this possible... ?

I tried this, as cell content:

%fs rm /FileStore/tables/file.txt
%fs ls /FileStore/tables/

and also this:

%%fs 
rm /FileStore/tables/file.txt
ls /FileStore/tables/

...and just in case...

%fs 
rm /FileStore/tables/file.txt
ls /FileStore/tables/

Having the rm and ls commands in different cells works, but is there a way to have them both in the same cell...?


Solution

  • You can't do that for %fs - it treats the rest as arguments for the first command. The same for the other magic commands.

    If you want to execute multiple commands in one cell, then you need to use dbutils.fs... commands in Python or Scala (doc):

    dbutils.fs.rm("/FileStore/tables/file.txt")
    dbutils.fs.ls("/FileStore/tables/")