From 2cad4fcd7f3d78a0dcf734d49257b8e2ebd5f142 Mon Sep 17 00:00:00 2001 From: Woose Date: Sat, 25 Jan 2025 01:10:25 +0300 Subject: [PATCH] update --- module4.ipynb | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 95 insertions(+), 1 deletion(-) diff --git a/module4.ipynb b/module4.ipynb index 07ef68f..f07f088 100644 --- a/module4.ipynb +++ b/module4.ipynb @@ -152,13 +152,107 @@ "\n", "./parameters.py one two three\n", "['./parameters.py', 'one', 'two', 'three']\n", - "…cat example \n", + "\n", + "\n", + "wc variables.py\n", + "7 19 174 variables.py \t\n", + "echo $?\n", + "0\n", + "\n", + "wc notpresent.sh\n", + "wc: notpresent.sh: No such file or directory\n", + "echo $?\n", + "1\n", + "\n", + "#!/usr/bin/env python3\n", + "\n", + "import os\n", + "import sys\n", + "\n", + "filename = sys.argv[1]\n", + "\n", + "if not os.path.exists(filename):\n", + " with open(filename, \"w\") as f:\n", + " f.write(\"New file created\\n\")\n", + "else:\n", + " print(\"Error, the file {} already exists!\".format(filename))\n", + " sys.exit(1)\n", + "\n", + "./create_file.py example\n", + "echo $?\n", + "0\n", + "\n", + "cat example \n", "New file created\n", "./create_file.py example\n", "Error, the file example already exists!\n", "echo $?\n", "1" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Python 2 and Python 3 handle input and raw_input differently.\n", + "\n", + "In Python 2\n", + "\n", + "- input(x) is roughly the same as eval(raw_input(x))\n", + "- raw_input() is preferred, unless the author wants to support evaluating string expressions.\n", + "- eval() is used to evaluate string expressions." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + ">>> my_number = input('Please Enter a Number: \\n')\n", + "Please Enter a Number: \n", + "123 + 1\n", + ">>> print(my_number)\n", + "123 + 1\n", + ">>> type(my_number)\n", + "\n", + "\n", + "\n", + ">>> my_number = input('Please Enter a Number: \\n')\n", + "Please Enter a Number: \n", + "123 + 1\n", + ">>> print(my_number)\n", + "123 + 1\n", + ">>> eval(my_number)\n", + "124" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Subprocesses" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Running system commands in Python" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import subprocess\n", + "subprocess.run([\"date\"])\n", + "subprocess.run([\"sleep\", \"2\"])\n", + "result = subprocess.run([\"ls\", \"this_file_does_not_exist\"])\n", + "print(result.returncode)" + ] } ], "metadata": {