{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Manipulating the command line" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "import subprocess\n", "\n", "import shutil" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# OS" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['GEN2212.dtardif',\n", " 'GEN2212.qpillot',\n", " 'GEN2212.wbanfield',\n", " 'GEN2212.atoumoulin',\n", " 'GEN2212.apohl',\n", " 'GEN2212.mlaugie',\n", " 'GEN2212.ydonnadieu',\n", " 'GEN2212.asarr',\n", " 'maxs_mins_consecutive.ipynb',\n", " 'GEN2212.jsayago']" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# See contents of directory\n", "os.listdir('/mnt/GEN2212/')" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Directory:\t GEN2212.dtardif\n", "Directory:\t GEN2212.qpillot\n", "Directory:\t GEN2212.wbanfield\n", "Directory:\t GEN2212.atoumoulin\n", "Directory:\t GEN2212.apohl\n", "Directory:\t GEN2212.mlaugie\n", "Directory:\t GEN2212.ydonnadieu\n", "Directory:\t GEN2212.asarr\n", "File:\t maxs_mins_consecutive.ipynb\n", "Directory:\t GEN2212.jsayago\n" ] } ], "source": [ "# Loop over objects in directory\n", "for obj in os.listdir('/mnt/GEN2212/'):\n", " # See if it is a directory\n", " if os.path.isdir('/mnt/GEN2212/' + obj):\n", " print(\"Directory:\\t\", obj)\n", " else:\n", " print(\"File:\\t\", obj)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'/home/wbanfield'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Acces enviroment variables\n", "os.environ['HOME']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "os.listdir(os.environ['HOME'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Subprocess" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The cell below only works in jupyter " ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "total 27945\n", "drwxrwx--- 11 root CEREGE_GEN2212 12 Apr 8 10:47 .\n", "drwxr-xr-x 3 root root 4096 Sep 21 2018 ..\n", "drwxr-x--x 18 apohl CEREGE_GEN2212 20 Feb 26 10:09 GEN2212.apohl\n", "drwxr-xr-x 10 asarr CEREGE_GEN2212 20 May 5 17:51 GEN2212.asarr\n", "drwxr-x--x 2 atoumoulin CEREGE_GEN2212 2 Sep 21 2018 GEN2212.atoumoulin\n", "drwxr-xr-x 5 dtardif CEREGE_GEN2212 5 May 7 11:36 GEN2212.dtardif\n", "drwxr-xr-x 5 jsayago CEREGE_GEN2212 5 Apr 26 13:45 GEN2212.jsayago\n", "drwxr-x--x 4 mlaugie CEREGE_GEN2212 4 Apr 2 11:42 GEN2212.mlaugie\n", "drwxr-xr-x 5 qpillot CEREGE_GEN2212 5 Mar 22 10:45 GEN2212.qpillot\n", "drwxr-xr-x 11 wbanfield CEREGE_GEN2212 18 Apr 23 12:41 GEN2212.wbanfield\n", "drwxr-x--x 7 ydonnadieu CEREGE_GEN2212 22 Apr 16 19:28 GEN2212.ydonnadieu\n", "-rw-r--r-- 1 jsayago CEREGE_GEN2212 28225795 Apr 8 10:46 maxs_mins_consecutive.ipynb\n" ] } ], "source": [ "!ls -al /mnt/GEN2212/" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A Few libraries exist:\n", "- os.popen\n", "- sys.?\n", "- subprocess\n", "\n", "However subprocess seems to be the most built out for \"complex\" tasks.\n", "\n", "To get the results use `stdout=subprocess.PIPE` then `output.stdout.readlines()`" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "output = subprocess.Popen(['ls', '-al', '/mnt/GEN2212'], stdout=subprocess.PIPE)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[b'total 27945\\n',\n", " b'drwxrwx--- 11 root CEREGE_GEN2212 12 Apr 8 10:47 .\\n',\n", " b'drwxr-xr-x 3 root root 4096 Sep 21 2018 ..\\n',\n", " b'drwxr-x--x 18 apohl CEREGE_GEN2212 20 Feb 26 10:09 GEN2212.apohl\\n',\n", " b'drwxr-xr-x 10 asarr CEREGE_GEN2212 20 May 5 17:51 GEN2212.asarr\\n',\n", " b'drwxr-x--x 2 atoumoulin CEREGE_GEN2212 2 Sep 21 2018 GEN2212.atoumoulin\\n',\n", " b'drwxr-xr-x 5 dtardif CEREGE_GEN2212 5 May 7 11:36 GEN2212.dtardif\\n',\n", " b'drwxr-xr-x 5 jsayago CEREGE_GEN2212 5 Apr 26 13:45 GEN2212.jsayago\\n',\n", " b'drwxr-x--x 4 mlaugie CEREGE_GEN2212 4 Apr 2 11:42 GEN2212.mlaugie\\n',\n", " b'drwxr-xr-x 5 qpillot CEREGE_GEN2212 5 Mar 22 10:45 GEN2212.qpillot\\n',\n", " b'drwxr-xr-x 11 wbanfield CEREGE_GEN2212 18 Apr 23 12:41 GEN2212.wbanfield\\n',\n", " b'drwxr-x--x 7 ydonnadieu CEREGE_GEN2212 22 Apr 16 19:28 GEN2212.ydonnadieu\\n',\n", " b'-rw-r--r-- 1 jsayago CEREGE_GEN2212 28225795 Apr 8 10:46 maxs_mins_consecutive.ipynb\\n']" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lines = output.stdout.readlines()\n", "lines" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can then manipulate the output like strings to get info" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "drwxr-xr-x 11 wbanfield CEREGE_GEN2212 18 Apr 23 12:41 GEN2212.wbanfield\n", "\n", "['drwxr-xr-x', '11', 'wbanfield', 'CEREGE_GEN2212', '18', 'Apr', '23', '12:41', 'GEN2212.wbanfield\\n']\n" ] } ], "source": [ "for line in lines:\n", " line = line.decode('utf-8')\n", " if \"wbanfield\" in line:\n", " print(line)\n", " line = line.split(\" \")\n", " line = [obj for obj in line if len(obj) > 0]\n", " print(line)\n", " perm, uid, user, group, day, month, _, time, directory = line" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'drwxr-xr-x'" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "perm" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'wbanfield'" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "user" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`.wait()` waits for a process to finish before continuing to the next one.\n", "`cwd` argument defines the directory from which the command is executed" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [], "source": [ "proc1 = subprocess.Popen([\"sleep\", \"2\"], cwd=\"/mnt/GEN2212\").wait()\n", "proc2 = subprocess.Popen([\"sleep\", \"3\"], cwd=\"/mnt\").wait()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# File manipulation\n", "\n", "- 'r' -> read only\n", "- 'a' -> create if doesn't exist else append\n", "- 'w' -> write this overwrites the file if it exists" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " ! NOAA/PMEL TMAP\n", "\n", " ! PyFerret v7.63 (optimized)\n", "\n", " ! Linux 4.15.0-1096-azure - 10/13/20\n", "\n", " ! 15-Apr-21 13:30 \n", "\n", "\n", "\n", "set mode verify\n", "\n", "no\n", "\n", "exit\n", "\n" ] } ], "source": [ "with open(\"ferret.jnl\", \"r\") as f:\n", " print(\"\\n\".join(f.readlines()))" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "User : wbanfield owns the Folder GEN2212.wbanfield\n", "\n" ] } ], "source": [ "with open(\"dummy.txt\", 'w') as f:\n", " print(f\"User : {user} owns the Folder {directory}\")\n", " f.write(f\"User : {user} owns the Folder {directory}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Shutil" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Remove all objects under given directory -> equivalent to rm -rf\n", "shutil.rmtree(\"DIRECTORY\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Copy files (and folders?)\n", "shutil.copy2(source, dest)" ] } ], "metadata": { "kernelspec": { "display_name": "Python (pangeo)", "language": "python", "name": "python-pangeo" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.9" } }, "nbformat": 4, "nbformat_minor": 4 }