Skip to content

Instantly share code, notes, and snippets.

@ischurov
Created February 5, 2021 00:18
Show Gist options
  • Save ischurov/6ea00f3e6db516e0f1d164f107a1d865 to your computer and use it in GitHub Desktop.
Save ischurov/6ea00f3e6db516e0f1d164f107a1d865 to your computer and use it in GitHub Desktop.
lesson07.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "## лямбда-функции",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "table = [[1, 4, 3],\n [2, 3, 4],\n [0, 2, 5]]\n\ndef get_second_element(row):\n return row[1]\n\nsorted(table, key=get_second_element)",
"execution_count": 1,
"outputs": [
{
"data": {
"text/plain": "[[0, 2, 5], [2, 3, 4], [1, 4, 3]]"
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def subs2(f):\n return f(2)",
"execution_count": 2,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "subs2(lambda x: x * 3)",
"execution_count": 4,
"outputs": [
{
"data": {
"text/plain": "6"
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "(lambda x: x * 3)(5)",
"execution_count": 5,
"outputs": [
{
"data": {
"text/plain": "15"
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def mult_3(x):\n return x * 3\nmult_3(5)",
"execution_count": 6,
"outputs": [
{
"data": {
"text/plain": "15"
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "sorted(table, key=lambda row: row[1])",
"execution_count": 9,
"outputs": [
{
"data": {
"text/plain": "[[0, 2, 5], [2, 3, 4], [1, 4, 3]]"
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "(lambda x: x if x > 0 else -x)(-5)",
"execution_count": 12,
"outputs": [
{
"data": {
"text/plain": "5"
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "(lambda x: [i ** 2 for i in range(x) if i % 3 == 0])(20)",
"execution_count": 14,
"outputs": [
{
"data": {
"text/plain": "[0, 9, 36, 81, 144, 225, 324]"
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "(lambda x, f: f(x))(10, lambda x: x ** 2)",
"execution_count": 15,
"outputs": [
{
"data": {
"text/plain": "100"
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "from operator import itemgetter",
"execution_count": 16,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "sorted(table, key=itemgetter(1))",
"execution_count": 18,
"outputs": [
{
"data": {
"text/plain": "[[0, 2, 5], [2, 3, 4], [1, 4, 3]]"
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "itemgetter(1)([100, 200, 300, 400])",
"execution_count": 20,
"outputs": [
{
"data": {
"text/plain": "200"
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "itemgetter(2)([100, 200, 300, 400])",
"execution_count": 21,
"outputs": [
{
"data": {
"text/plain": "300"
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "itemgetter(1, 3)([100, 200, 300, 400])",
"execution_count": 24,
"outputs": [
{
"data": {
"text/plain": "(200, 400)"
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "incrementors = []\nfor i in range(10):\n incrementors.append(lambda x: x + i)",
"execution_count": 25,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "incrementors[0](20)",
"execution_count": 28,
"outputs": [
{
"data": {
"text/plain": "29"
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "incrementors[3](20)",
"execution_count": 27,
"outputs": [
{
"data": {
"text/plain": "29"
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "i = 50",
"execution_count": 29,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "incrementors[3](20)",
"execution_count": 30,
"outputs": [
{
"data": {
"text/plain": "70"
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "incrementors = []\nfor i in range(10):\n incrementors.append(lambda x, inc=i: x + inc)",
"execution_count": 31,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "incrementors[3](7)",
"execution_count": 32,
"outputs": [
{
"data": {
"text/plain": "10"
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "incrementors[5](2)",
"execution_count": 34,
"outputs": [
{
"data": {
"text/plain": "7"
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "incrementors[5](2, inc=200)",
"execution_count": 37,
"outputs": [
{
"data": {
"text/plain": "202"
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "## упражнение\n## напишите itemgetter",
"execution_count": 38,
"outputs": []
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## Множества"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "A = {1, 2, 3}\nB = {3, 2, 2, 2, 'a'}",
"execution_count": 115,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "A",
"execution_count": 116,
"outputs": [
{
"data": {
"text/plain": "{1, 2, 3}"
},
"execution_count": 116,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "B",
"execution_count": 117,
"outputs": [
{
"data": {
"text/plain": "{2, 3, 'a'}"
},
"execution_count": 117,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "A | B",
"execution_count": 118,
"outputs": [
{
"data": {
"text/plain": "{1, 2, 3, 'a'}"
},
"execution_count": 118,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "A & B",
"execution_count": 119,
"outputs": [
{
"data": {
"text/plain": "{2, 3}"
},
"execution_count": 119,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "1 in A",
"execution_count": 120,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 120,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "for i in sorted(A):\n print(i)",
"execution_count": 124,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "1\n2\n3\n"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## Пример"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Пусть мы хотим убедиться, что среди чисел `x`, `y` и `z` нет одинаковых."
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x = 1\ny = 2\nz = 4\nif x != y and x != z and y != z:\n print(\"All different\")",
"execution_count": 46,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "All different\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "if len({x, y, z}) == 3:\n print(\"All different\")",
"execution_count": 47,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "All different\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "set({1, 2, 3})[1]",
"execution_count": 48,
"outputs": [
{
"ename": "TypeError",
"evalue": "'set' object does not support indexing",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-48-86da26f02818>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mset\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m{\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: 'set' object does not support indexing"
]
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "list(set({1, 2, 3}))[0]",
"execution_count": 49,
"outputs": [
{
"data": {
"text/plain": "1"
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "{'a', [2]}",
"execution_count": 50,
"outputs": [
{
"ename": "TypeError",
"evalue": "unhashable type: 'list'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-50-7882af34050d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;34m{\u001b[0m\u001b[0;34m'a'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: unhashable type: 'list'"
]
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "{(1, 2), (3, 4)}",
"execution_count": 51,
"outputs": [
{
"data": {
"text/plain": "{(1, 2), (3, 4)}"
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## Немного про строки"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "s = \"Hello, world!\"",
"execution_count": 52,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "s.replace(\",\", \"!\")",
"execution_count": 53,
"outputs": [
{
"data": {
"text/plain": "'Hello! world!'"
},
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "s.replace(\"lo\", \"LOL\")",
"execution_count": 54,
"outputs": [
{
"data": {
"text/plain": "'HelLOL, world!'"
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "s.replace(\"l\", \"L\", 1)",
"execution_count": 56,
"outputs": [
{
"data": {
"text/plain": "'HeLlo, world!'"
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "s.startswith(\"He\")",
"execution_count": 57,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "s.endswith(\"!\")",
"execution_count": 58,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "\"abba\".translate({'a': 'X', 'b': 'Y'})",
"execution_count": 59,
"outputs": [
{
"data": {
"text/plain": "'abba'"
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "s[2:9]",
"execution_count": 63,
"outputs": [
{
"data": {
"text/plain": "'llo, wo'"
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "import os",
"execution_count": 65,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "os.getcwd()",
"execution_count": 66,
"outputs": [
{
"data": {
"text/plain": "'/Users/user/prj/svn.math-hse.info/repo/2020-21/nes-datascience'"
},
"execution_count": 66,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "f = open(\"untitled.txt\")\nfor line in f:\n print(line.rstrip())\nf.close()",
"execution_count": 71,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "Раз два три четыре пять.\nВышел зайчик погулять.\nОй-ой-ой.\nHello, world!\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "line",
"execution_count": 68,
"outputs": [
{
"data": {
"text/plain": "'Hello, world!\\n'"
},
"execution_count": 68,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "print(\"Hello\", end=\" \")\nprint(\"World\")",
"execution_count": 69,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "Hello World\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "print(\"Hello\")\nprint(\"World\")",
"execution_count": 70,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "Hello\nWorld\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "f = open(\"untitled.txt\")\nfor line in f:\n print(line.rstrip())\nprint(\"== Once again ==\")\nfor line in f:\n print(line.rstrip())\nf.close()",
"execution_count": 73,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "Раз два три четыре пять.\nВышел зайчик погулять.\nОй-ой-ой.\nHello, world!\n== Once again ==\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "f = open(\"untitled.txt\")\nfor line in f:\n print(line.rstrip())\nprint(\"== Once again ==\")\nf.seek(0)\nfor line in f:\n print(line.rstrip())\nf.close()",
"execution_count": 74,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "Раз два три четыре пять.\nВышел зайчик погулять.\nОй-ой-ой.\nHello, world!\n== Once again ==\nРаз два три четыре пять.\nВышел зайчик погулять.\nОй-ой-ой.\nHello, world!\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "f = open(\"untitled.txt\")\nfor line in f:\n print(line.rstrip())\nprint(\"== Once again ==\")\nf.close()\nf = open(\"untitled.txt\")\nfor line in f:\n print(line.rstrip())\nf.close()",
"execution_count": 75,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "Раз два три четыре пять.\nВышел зайчик погулять.\nОй-ой-ой.\nHello, world!\n== Once again ==\nРаз два три четыре пять.\nВышел зайчик погулять.\nОй-ой-ой.\nHello, world!\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "f = open(\"untitled.txt\")\nfor line in f:\n print(line.rstrip())\nprint(\"== Once again ==\")\nf.seek(2)\nfor line in f:\n print(line.rstrip())\nf.close()",
"execution_count": 77,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "Раз два три четыре пять.\nВышел зайчик погулять.\nОй-ой-ой.\nHello, world!\n== Once again ==\nаз два три четыре пять.\nВышел зайчик погулять.\nОй-ой-ой.\nHello, world!\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "f = open(\"untitled.txt\", encoding='CP1251')\nfor line in f:\n print(line.rstrip())\nprint(\"== Once again ==\")\nf.seek(2)\nfor line in f:\n print(line.rstrip())\nf.close()",
"execution_count": 78,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "Раз два три четыре пять.\nВышел зайчик погулять.\nОй-ой-ой.\nHello, world!\n== Once again ==\nаз два три четыре пять.\nВышел зайчик погулять.\nОй-ой-ой.\nHello, world!\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "f = open(\"untitled.txt\")\nfor line in f:\n lines = f.readlines()\nf.close()",
"execution_count": 79,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "lines",
"execution_count": 80,
"outputs": [
{
"data": {
"text/plain": "['Вышел зайчик погулять.\\n', 'Ой-ой-ой.\\n', 'Hello, world!\\n']"
},
"execution_count": 80,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "f = open(\"untitled.txt\")\nfor line in f:\n lines = f.read()\nf.close()",
"execution_count": 81,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "lines",
"execution_count": 82,
"outputs": [
{
"data": {
"text/plain": "'Вышел зайчик погулять.\\nОй-ой-ой.\\nHello, world!\\n'"
},
"execution_count": 82,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "with open(\"untitled.txt\") as f:\n lines = f.readlines()\n print(\"We read the file\")\n print(f.closed)\n print(\"It's still open\")\nprint(\"Now it's closed\")\nprint(f.closed)",
"execution_count": 83,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "We read the file\nFalse\nIt's still open\nNow it's closed\nTrue\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "f.readlines()",
"execution_count": 84,
"outputs": [
{
"ename": "ValueError",
"evalue": "I/O operation on closed file.",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-84-055d0c37aeda>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreadlines\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mValueError\u001b[0m: I/O operation on closed file."
]
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "with open(\"newfile.txt\", \"w\") as f:\n print(\"Этот файл написал искусственный интеллект\", file=f)\n print(\"Humans, beware!\", file=f)",
"execution_count": 85,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "with open(\"newfile.txt\", \"a\") as f:\n print(\"Skynet is coming!\", file=f)",
"execution_count": 86,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "from pathlib import Path",
"execution_count": 87,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "for i in range(1, 12):\n print(f\"dir{i:03}\")",
"execution_count": 92,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "dir001\ndir002\ndir003\ndir004\ndir005\ndir006\ndir007\ndir008\ndir009\ndir010\ndir011\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "for i in range(1, 11):\n Path(f\"dir{i:02}\").mkdir(exist_ok=True)",
"execution_count": 95,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "for i in range(1, 11):\n with open(Path(f\"dir{i:02}\") / \"myfile.txt\", \"w\") as f:\n print(f\"This is file in folder {i}\", file=f)",
"execution_count": 100,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "Path(f\"dir{i:02}\") / \"myfile.txt\"",
"execution_count": 98,
"outputs": [
{
"data": {
"text/plain": "PosixPath('dir10/myfile.txt')"
},
"execution_count": 98,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "Path(\"a\") / \"b\" / \"c\"",
"execution_count": 99,
"outputs": [
{
"data": {
"text/plain": "PosixPath('a/b/c')"
},
"execution_count": 99,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "p = Path(\"dir\") / \"subdir\" / \"file.txt\"",
"execution_count": 101,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "p.exists()",
"execution_count": 103,
"outputs": [
{
"data": {
"text/plain": "False"
},
"execution_count": 103,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "Path(\"..\") / \"..\"",
"execution_count": 105,
"outputs": [
{
"data": {
"text/plain": "PosixPath('../..')"
},
"execution_count": 105,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "os.getcwd()",
"execution_count": 106,
"outputs": [
{
"data": {
"text/plain": "'/Users/user/prj/svn.math-hse.info/repo/2020-21/nes-datascience'"
},
"execution_count": 106,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "(Path(\"dir02\") / \"myfile.txt\").read_text()",
"execution_count": 109,
"outputs": [
{
"data": {
"text/plain": "'This is file in folder 2\\n'"
},
"execution_count": 109,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "for file in Path(\".\").iterdir():\n if file.is_dir():\n print(file)",
"execution_count": 113,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "assignments\ndir01\ndir06\ndir08\ndir09\ndir07\nold\ndir10\n.ipynb_checkpoints\ndir05\ndir02\ndir03\ndir04\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.6.10",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"gist": {
"id": "",
"data": {
"description": "lesson07.ipynb",
"public": false
}
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment