This commit is contained in:
Yavuz Sava 2025-01-25 01:10:25 +03:00
parent fd3cafeaaa
commit 2cad4fcd7f

View File

@ -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",
"<class 'str'>\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": {