Using_Python_to_Interact_wi.../module6.ipynb
2025-02-01 00:41:31 +03:00

203 lines
6.3 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Interacting with the Command Line Shell"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Basic Linux Commands"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"mkdir mynewdir\n",
"cd mynewdir/\n",
"/mynewdir$ pwd\n",
"/mynewdir$ cp ../spider.txt .\n",
"/mynewdir$ touch myfile.txt\n",
"/mynewdir$ ls -l \n",
"#Output:\n",
"#-rw-rw-r-- 1 user user 0 Mai 22 14:22 myfile.txt\n",
"#-rw-rw-r-- 1 user user 192 Mai 22 14:18 spider.txt\n",
"/mynewdir$ ls -la\n",
"#Output:\n",
"#total 12\n",
"#drwxr-xr-x 2 user user 4096 Mai 22 14:17 .\n",
"#drwxr-xr-x 56 user user 12288 Mai 22 14:17 ..\n",
"#-rw-rw-r-- 1 user user 0 Mai 22 14:22 myfile.txt\n",
"#-rw-rw-r-- 1 user user 192 Mai 22 14:18 spider.txt\n",
"/mynewdir$ mv myfile.txt emptyfile.txt\n",
"/mynewdir$ cp spider.txt yetanotherfile.txt\n",
"/mynewdir$ ls -l\n",
"#Output:\n",
"#total 8\n",
"#-rw-rw-r-- 1 user user 0 Mai 22 14:22 emptyfile.txt\n",
"#-rw-rw-r-- 1 user user 192 Mai 22 14:18 spider.txt\n",
"#-rw-rw-r-- 1 user user 192 Mai 22 14:23 yetanotherfile.txt\n",
"/mynewdir$ rm *\n",
"/mynewdir$ ls -l\n",
"#total 0\n",
"/mynewdir$ cd ..\n",
"rmdir mynewdir/\n",
"ls mynewdir\n",
"#ls: cannot access 'mynewdir': No such file or directory\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Redirecting streams"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"cat stdout_example.py \n",
"#!/usr/bin/env python3\n",
"print(\"Don't mind me, just a bit of text here...\")\n",
"./stdout_example.py \n",
"#Output: Don't mind me, just a bit of text here...\n",
"./stdout_example.py > new_file.txt\n",
"cat new_file.txt \n",
"#Output: Don't mind me, just a bit of text here...\n",
"./stdout_example.py >> new_file.txt\n",
"cat new_file.txt \n",
"#Output: Don't mind me, just a bit of text here...\n",
" #Don't mind me, just a bit of text here...\n",
"cat streams_err.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",
"raise ValueError(\"Now we generate an error to STDERR\")\n",
"./streams_err.py < new_file.txt\n",
"#This will come from STDIN: Now we write it to STDOUT: Don't mind #me, just a bit of text here...\n",
"#Traceback (most recent call last):\n",
" #File \"./streams_err.py\", line 5, in <module>\n",
" #raise ValueError(\"Now we generate an error to STDERR\")\n",
"#ValueError: Now we generate an error to STDERR\n",
"./streams_err.py < new_file.txt 2> error_file.txt\n",
"#This will come from STDIN: Now we write it to STDOUT: Don't mind #me, just a bit of text here...\n",
"cat error_file.txt \n",
"#Traceback (most recent call last):\n",
" #File \"./streams_err.py\", line 5, in <module>\n",
" #raise ValueError(\"Now we generate an error to STDERR\")\n",
"#ValueError: Now we generate an error to STDERR\n",
"echo \"These are the contents of the file\" > myamazingfile.txt\n",
"cat myamazingfile.txt \n",
"#These are the contents of the file\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Pipes and pipelines"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ls -l | less\n",
"#(... A list of files appears...)\n",
"# It lists all files and directories in the current directory in long format (including permissions, number of links, owner, group, size, and modification date). The output is then piped into \"less\" command which allows you to page through the results one screen at a time.\n",
"# This code does the following: \n",
"# 1. cat spider.txt: Reads the contents of the file \"spider.txt\"\n",
"# 2. tr ' ' '\\n': Uses the 'tr' command to replace all spaces in the text with newline characters. This transforms each word into a separate line, preparing it for sorting and counting unique occurrences.\n",
"# 3. sort: Sorts the lines (words) alphabetically\n",
"# 4. uniq -c: Counts the number of occurrences of each line (word), prefixing each line with the count\n",
"# 5. sort -nr: Sorts the lines in numerical and reverse order (i.e., descending order) based on the counts from the \"uniq -c\" command. This will put the most frequent words at the top.\n",
"# 6. head: Displays only the first few lines of the sorted output. By default it shows the first line but here we may change this number to view more results.\n",
"cat spider.txt | tr ' ' '\\n' | sort | uniq -c | sort -nr | head\n",
"# This part does the following:\n",
" # 7 the\n",
" # 3 up\n",
" # 3 spider\n",
" # 3 and\n",
" # 2 rain\n",
" # 2 itsy\n",
" # 2 climbed\n",
" # 2 came\n",
" # 2 bitsy\n",
" # 1 waterspout."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"cat capitalize.py\n",
"#!/usr/bin/env python3\n",
"\n",
"import sys\n",
"\n",
"for line in sys.stdin:\n",
" print(line.strip().capitalize())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"cat haiku.txt \n",
"#advance your career,\n",
"#automating with Python,\n",
"#it's so fun to learn.\n",
"\n",
"cat haiku.txt | ./capitalize.py \n",
"#Advance your career,\n",
"#Automating with python,\n",
"#It's so fun to learn.\n",
"\n",
"./capitalize.py < haiku.txt\n",
"#Advance your career,\n",
"#Automating with python,\n",
"#It's so fun to learn."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}