Skip to content

Commit 4bb5559

Browse files
committed
Add Shutil and os notebook
1 parent 3338f17 commit 4bb5559

29 files changed

+163
-0
lines changed

Shutil_OS.ipynb

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
{
2+
"metadata": {
3+
"language_info": {
4+
"codemirror_mode": {
5+
"name": "ipython",
6+
"version": 3
7+
},
8+
"file_extension": ".py",
9+
"mimetype": "text/x-python",
10+
"name": "python",
11+
"nbconvert_exporter": "python",
12+
"pygments_lexer": "ipython3",
13+
"version": "3.8.6-final"
14+
},
15+
"orig_nbformat": 2,
16+
"kernelspec": {
17+
"name": "python3",
18+
"display_name": "Python 3"
19+
}
20+
},
21+
"nbformat": 4,
22+
"nbformat_minor": 2,
23+
"cells": [
24+
{
25+
"cell_type": "code",
26+
"execution_count": 30,
27+
"metadata": {},
28+
"outputs": [
29+
{
30+
"output_type": "stream",
31+
"name": "stdout",
32+
"text": [
33+
"Python version: 3.8.6\n"
34+
]
35+
}
36+
],
37+
"source": [
38+
"import sys\n",
39+
"print('Python version: ' + sys.version[:5])\n",
40+
"\n",
41+
"import os\n",
42+
"import os.path\n",
43+
"import shutil\n",
44+
"from pathlib import Path"
45+
]
46+
},
47+
{
48+
"cell_type": "code",
49+
"execution_count": 39,
50+
"metadata": {},
51+
"outputs": [
52+
{
53+
"output_type": "stream",
54+
"name": "stdout",
55+
"text": [
56+
"['xpto.txt', 'xpto_1.txt', 'xpto_2.txt', 'xpto_3.txt']\n"
57+
]
58+
}
59+
],
60+
"source": [
61+
"os.chdir(\"C:\\Projects\\LearningPythonLibs\")\n",
62+
"\n",
63+
"path = lambda x: \"path_exercises/\" + x\n",
64+
"\n",
65+
"# Creates path if it doesn't exist\n",
66+
"if not os.path.exists(path('1')):\n",
67+
" os.mkdir(path('1'))\n",
68+
"\n",
69+
"# Change dir\n",
70+
"os.chdir(\"path_exercises/1\")\n",
71+
"\n",
72+
"\n",
73+
"# Creates xpto file\n",
74+
"Path(\"xpto.txt\").touch()\n",
75+
"\n",
76+
"# \n",
77+
"for el in range(1, 4):\n",
78+
" shutil.copy(\"xpto.txt\", f\"xpto_{el}.txt\")\n",
79+
"\n",
80+
"exercise1_files = os.listdir('.')\n",
81+
"print(exercise1_files)\n",
82+
"assert len(exercise1_files) == 4"
83+
]
84+
},
85+
{
86+
"cell_type": "code",
87+
"execution_count": 54,
88+
"metadata": {},
89+
"outputs": [
90+
{
91+
"output_type": "execute_result",
92+
"data": {
93+
"text/plain": [
94+
"['live_1.txt', 'live_2.txt', 'live_3.txt', 'live_4.txt']"
95+
]
96+
},
97+
"metadata": {},
98+
"execution_count": 54
99+
}
100+
],
101+
"source": [
102+
"os.chdir(\"C:\\Projects\\LearningPythonLibs\")\n",
103+
"\n",
104+
"if not os.path.exists(path('2')):\n",
105+
" os.mkdir(path('2'))\n",
106+
"\n",
107+
"os.chdir(\"path_exercises/2\")\n",
108+
"\n",
109+
"for el in range(10):\n",
110+
" Path(f\"live_{el}.txt\").touch()\n",
111+
"\n",
112+
"exercise2_files = os.listdir('.')\n",
113+
"\n",
114+
"for _file in exercise2_files:\n",
115+
" if int(_file.partition('_')[2][0]) <= 5:\n",
116+
" os.remove(_file)\n",
117+
"\n",
118+
"exercise2_files = os.listdir('.')\n",
119+
"\n",
120+
"for index, el in enumerate(sorted(exercise2_files), 1):\n",
121+
" shutil.move(el, f\"live_{index}.txt\")\n",
122+
"\n",
123+
"os.listdir('.')"
124+
]
125+
},
126+
{
127+
"cell_type": "code",
128+
"execution_count": 61,
129+
"metadata": {},
130+
"outputs": [
131+
{
132+
"output_type": "stream",
133+
"name": "stdout",
134+
"text": [
135+
"('.', ['dir_1', 'dir_10', 'dir_2', 'dir_3', 'dir_4', 'dir_5', 'dir_6', 'dir_7', 'dir_8', 'dir_9'], [])\n('.\\\\dir_1', [], ['file_0.txt', 'file_1.txt'])\n('.\\\\dir_10', [], ['file_0.txt', 'file_1.txt'])\n('.\\\\dir_2', [], ['file_0.txt', 'file_1.txt'])\n('.\\\\dir_3', [], ['file_0.txt', 'file_1.txt'])\n('.\\\\dir_4', [], ['file_0.txt', 'file_1.txt'])\n('.\\\\dir_5', [], ['file_0.txt', 'file_1.txt'])\n('.\\\\dir_6', [], ['file_0.txt', 'file_1.txt'])\n('.\\\\dir_7', [], ['file_0.txt', 'file_1.txt'])\n('.\\\\dir_8', [], ['file_0.txt', 'file_1.txt'])\n('.\\\\dir_9', [], ['file_0.txt', 'file_1.txt'])\n"
136+
]
137+
}
138+
],
139+
"source": [
140+
"os.chdir(\"C:\\Projects\\LearningPythonLibs\")\n",
141+
"\n",
142+
"if not os.path.exists(path(\"3\")):\n",
143+
" os.mkdir(path(\"3\"))\n",
144+
"\n",
145+
"os.chdir(\"path_exercises/3\")\n",
146+
"\n",
147+
"for x in range(1, 11):\n",
148+
" _dir = f\"dir_{x}\"\n",
149+
" if not os.path.exists(_dir): \n",
150+
" os.mkdir(_dir)\n",
151+
"\n",
152+
"dirs = os.listdir('.')\n",
153+
"\n",
154+
"for _dir in dirs:\n",
155+
" for i in range(2):\n",
156+
" Path(_dir + \"/file_\" + str(i) + \".txt\").touch()\n",
157+
"\n",
158+
"for val in os.walk(\".\"):\n",
159+
" print(val)"
160+
]
161+
}
162+
]
163+
}

path_exercises/1/xpto.txt

Whitespace-only changes.

path_exercises/1/xpto_1.txt

Whitespace-only changes.

path_exercises/1/xpto_2.txt

Whitespace-only changes.

path_exercises/1/xpto_3.txt

Whitespace-only changes.

path_exercises/2/live_1.txt

Whitespace-only changes.

path_exercises/2/live_2.txt

Whitespace-only changes.

path_exercises/2/live_3.txt

Whitespace-only changes.

path_exercises/2/live_4.txt

Whitespace-only changes.

path_exercises/3/dir_1/file_0.txt

Whitespace-only changes.

path_exercises/3/dir_1/file_1.txt

Whitespace-only changes.

path_exercises/3/dir_10/file_0.txt

Whitespace-only changes.

path_exercises/3/dir_10/file_1.txt

Whitespace-only changes.

path_exercises/3/dir_2/file_0.txt

Whitespace-only changes.

path_exercises/3/dir_2/file_1.txt

Whitespace-only changes.

path_exercises/3/dir_3/file_0.txt

Whitespace-only changes.

path_exercises/3/dir_3/file_1.txt

Whitespace-only changes.

path_exercises/3/dir_4/file_0.txt

Whitespace-only changes.

path_exercises/3/dir_4/file_1.txt

Whitespace-only changes.

path_exercises/3/dir_5/file_0.txt

Whitespace-only changes.

path_exercises/3/dir_5/file_1.txt

Whitespace-only changes.

path_exercises/3/dir_6/file_0.txt

Whitespace-only changes.

path_exercises/3/dir_6/file_1.txt

Whitespace-only changes.

path_exercises/3/dir_7/file_0.txt

Whitespace-only changes.

path_exercises/3/dir_7/file_1.txt

Whitespace-only changes.

path_exercises/3/dir_8/file_0.txt

Whitespace-only changes.

path_exercises/3/dir_8/file_1.txt

Whitespace-only changes.

path_exercises/3/dir_9/file_0.txt

Whitespace-only changes.

path_exercises/3/dir_9/file_1.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)