Search code examples
pythonbashjupyterrm

How to !rm python_var (in Jupyter notebooks)


I know I can do this:

CSV_Files = [file1.csv, file2.csv, etc...]

%rm file1.csv
!rm file2.csv

but how can I do this as a variable. eg.

TXT_Files = [ABC.txt, XYZ.txt, etc...]

for file in TXT_Files:
  !rm file

Solution

  • rm can remove several files per call:

    In [80]: !touch a.t1 b.t1 c.t1
    In [81]: !ls *.t1
    a.t1  b.t1  c.t1
    In [82]: !rm -r a.t1 b.t1 c.t1
    In [83]: !ls *.t1
    ls: cannot access '*.t1': No such file or directory
    

    If the starting point is a list of file names:

    In [116]: alist = ['a.t1', 'b.t1', 'c.t1']
    In [117]: astr = ' '.join(alist)            # make a string
    In [118]: !echo $astr                       # variable substitution as in BASH
    a.t1 b.t1 c.t1
    In [119]: !touch $astr                    # make 3 files
    In [120]: ls *.t1
    a.t1  b.t1  c.t1
    In [121]: !rm -r $astr                    # remove them
    In [122]: ls *.t1
    ls: cannot access '*.t1': No such file or directory
    

    Working with Python's own OS functions is probably better, but you can do much of the same stuff with %magics - if you understand shell well enough.


    To use 'magics' in a Python expression, I have to use the underlying functions, not the '!' or '%' syntax, e.g.

    import IPython
    for txt in ['a.t1','b.t1','c.t1']:
        IPython.utils.process.getoutput('touch %s'%txt)
    

    The getoutput function is used by %sx (which underlies !!) which uses subprocess.Popen. But if you go to all that work you might as well use the os functions that Python itself provides.


    The file names may need an added layer of quoting to ensure that the shell doesn't give a syntax error:

    In [129]: alist = ['"a(1).t1"', '"b(2).t1"', 'c.t1']
    In [130]: astr = ' '.join(alist)
    In [131]: !touch $astr
    In [132]: !ls *.t1
    'a(1).t1'   a.t1  'b(2).t1'   b.t1   c.t1