Manipulating the command line

import os

import subprocess

import shutil

OS

# See contents of directory
os.listdir('/mnt/GEN2212/')
['GEN2212.dtardif',
 'GEN2212.qpillot',
 'GEN2212.wbanfield',
 'GEN2212.atoumoulin',
 'GEN2212.apohl',
 'GEN2212.mlaugie',
 'GEN2212.ydonnadieu',
 'GEN2212.asarr',
 'maxs_mins_consecutive.ipynb',
 'GEN2212.jsayago']
# Loop over objects in directory
for obj in os.listdir('/mnt/GEN2212/'):
    # See if it is a directory
    if os.path.isdir('/mnt/GEN2212/' + obj):
        print("Directory:\t", obj)
    else:
        print("File:\t", obj)
Directory:	 GEN2212.dtardif
Directory:	 GEN2212.qpillot
Directory:	 GEN2212.wbanfield
Directory:	 GEN2212.atoumoulin
Directory:	 GEN2212.apohl
Directory:	 GEN2212.mlaugie
Directory:	 GEN2212.ydonnadieu
Directory:	 GEN2212.asarr
File:	 maxs_mins_consecutive.ipynb
Directory:	 GEN2212.jsayago
# Acces enviroment variables
os.environ['HOME']
'/home/wbanfield'
os.listdir(os.environ['HOME'])

Subprocess

The cell below only works in jupyter

!ls -al /mnt/GEN2212/
total 27945
drwxrwx--- 11 root       CEREGE_GEN2212       12 Apr  8 10:47 .
drwxr-xr-x  3 root       root               4096 Sep 21  2018 ..
drwxr-x--x 18 apohl      CEREGE_GEN2212       20 Feb 26 10:09 GEN2212.apohl
drwxr-xr-x 10 asarr      CEREGE_GEN2212       20 May  5 17:51 GEN2212.asarr
drwxr-x--x  2 atoumoulin CEREGE_GEN2212        2 Sep 21  2018 GEN2212.atoumoulin
drwxr-xr-x  5 dtardif    CEREGE_GEN2212        5 May  7 11:36 GEN2212.dtardif
drwxr-xr-x  5 jsayago    CEREGE_GEN2212        5 Apr 26 13:45 GEN2212.jsayago
drwxr-x--x  4 mlaugie    CEREGE_GEN2212        4 Apr  2 11:42 GEN2212.mlaugie
drwxr-xr-x  5 qpillot    CEREGE_GEN2212        5 Mar 22 10:45 GEN2212.qpillot
drwxr-xr-x 11 wbanfield  CEREGE_GEN2212       18 Apr 23 12:41 GEN2212.wbanfield
drwxr-x--x  7 ydonnadieu CEREGE_GEN2212       22 Apr 16 19:28 GEN2212.ydonnadieu
-rw-r--r--  1 jsayago    CEREGE_GEN2212 28225795 Apr  8 10:46 maxs_mins_consecutive.ipynb

A Few libraries exist:

  • os.popen

  • sys.?

  • subprocess

However subprocess seems to be the most built out for “complex” tasks.

To get the results use stdout=subprocess.PIPE then output.stdout.readlines()

output = subprocess.Popen(['ls', '-al', '/mnt/GEN2212'], stdout=subprocess.PIPE)
lines = output.stdout.readlines()
lines
[b'total 27945\n',
 b'drwxrwx--- 11 root       CEREGE_GEN2212       12 Apr  8 10:47 .\n',
 b'drwxr-xr-x  3 root       root               4096 Sep 21  2018 ..\n',
 b'drwxr-x--x 18 apohl      CEREGE_GEN2212       20 Feb 26 10:09 GEN2212.apohl\n',
 b'drwxr-xr-x 10 asarr      CEREGE_GEN2212       20 May  5 17:51 GEN2212.asarr\n',
 b'drwxr-x--x  2 atoumoulin CEREGE_GEN2212        2 Sep 21  2018 GEN2212.atoumoulin\n',
 b'drwxr-xr-x  5 dtardif    CEREGE_GEN2212        5 May  7 11:36 GEN2212.dtardif\n',
 b'drwxr-xr-x  5 jsayago    CEREGE_GEN2212        5 Apr 26 13:45 GEN2212.jsayago\n',
 b'drwxr-x--x  4 mlaugie    CEREGE_GEN2212        4 Apr  2 11:42 GEN2212.mlaugie\n',
 b'drwxr-xr-x  5 qpillot    CEREGE_GEN2212        5 Mar 22 10:45 GEN2212.qpillot\n',
 b'drwxr-xr-x 11 wbanfield  CEREGE_GEN2212       18 Apr 23 12:41 GEN2212.wbanfield\n',
 b'drwxr-x--x  7 ydonnadieu CEREGE_GEN2212       22 Apr 16 19:28 GEN2212.ydonnadieu\n',
 b'-rw-r--r--  1 jsayago    CEREGE_GEN2212 28225795 Apr  8 10:46 maxs_mins_consecutive.ipynb\n']

We can then manipulate the output like strings to get info

for line in lines:
    line = line.decode('utf-8')
    if "wbanfield" in line:
        print(line)
        line = line.split(" ")
        line = [obj for obj in line if len(obj) > 0]
        print(line)
        perm, uid, user, group, day, month, _, time, directory = line
drwxr-xr-x 11 wbanfield  CEREGE_GEN2212       18 Apr 23 12:41 GEN2212.wbanfield

['drwxr-xr-x', '11', 'wbanfield', 'CEREGE_GEN2212', '18', 'Apr', '23', '12:41', 'GEN2212.wbanfield\n']
perm
'drwxr-xr-x'
user
'wbanfield'

.wait() waits for a process to finish before continuing to the next one. cwd argument defines the directory from which the command is executed

proc1 = subprocess.Popen(["sleep", "2"], cwd="/mnt/GEN2212").wait()
proc2 = subprocess.Popen(["sleep", "3"], cwd="/mnt").wait()

File manipulation

  • ‘r’ -> read only

  • ‘a’ -> create if doesn’t exist else append

  • ‘w’ -> write this overwrites the file if it exists

with open("ferret.jnl", "r") as f:
    print("\n".join(f.readlines()))
 ! NOAA/PMEL TMAP

 ! PyFerret v7.63 (optimized)

 ! Linux 4.15.0-1096-azure - 10/13/20

 ! 15-Apr-21 13:30     



set mode verify

no

exit
with open("dummy.txt", 'w') as f:
    print(f"User : {user} owns the Folder {directory}")
    f.write(f"User : {user} owns the Folder {directory}")
User : wbanfield owns the Folder GEN2212.wbanfield

Shutil

# Remove all objects under given directory -> equivalent to rm -rf
shutil.rmtree("DIRECTORY")
# Copy files (and folders?)
shutil.copy2(source, dest)