This commit is contained in:
Yavuz Sava 2025-01-25 00:05:56 +03:00
parent 585f516863
commit fd3cafeaaa

View File

@ -58,11 +58,126 @@
"# print a goodbye message when user decides to stop the program.\n",
"print(\"Goodbye!\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Standard streams\n",
"I/O Streams"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"cat streams.py\n",
"#!/usr/bin/env python3\n",
"\n",
"data = input(\"This will come from STDIN: \")\n",
"print(\"Now we write it to STDOUT: \" + data)\n",
"print(\"Now we generate an error to STDERR: \" + data + 1)\n",
"\n",
"./streams.py \n",
"This will come from STDIN: Python Rocks!\n",
"Now we write it to STDOUT: Python Rocks!\n",
"\n",
"cat greeting.txt \n",
"Well hello there, STDOUT\n",
"\n",
"cat greeting.txt \n",
"Well hello there, STDOUT\n",
"\n",
"ls -z"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Environment variables"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"HOME: /config\n",
"SHELL: \n",
"FRUIT: \n"
]
}
],
"source": [
"# echo $PATH\n",
"# /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\n",
"# cat variables.py\n",
"#!/usr/bin/env python3\n",
"import os\n",
"print(\"HOME: \" + os.environ.get(\"HOME\", \"\"))\n",
"print(\"SHELL: \" + os.environ.get(\"SHELL\", \"\"))\n",
"print(\"FRUIT: \" + os.environ.get(\"FRUIT\", \"\"))\n",
"# ./variables.py\n",
"# export FRUIT=Pineapple\n",
"# ./variables.py"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Command-Line Arguments and Exit Status"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"cat parameters.py \n",
"#!/usr/bin/env python3\n",
"import sys\n",
"print(sys.argv)\n",
"\n",
"./parameters.py\n",
"['./parameters.py'] \n",
"\n",
"./parameters.py one two three\n",
"['./parameters.py', 'one', 'two', 'three']\n",
"…cat example \n",
"New file created\n",
"./create_file.py example\n",
"Error, the file example already exists!\n",
"echo $?\n",
"1"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python"
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,