@@ -20,67 +20,59 @@ Current directory:
20
20
21
21
.. ipython ::
22
22
23
- In [17]: os.getcwd()
24
- Out[17]: '/Users/cburns/src/scipy2009/scipy_2009_tutorial/source'
23
+ In [1]: import os
24
+
25
+ In [2]: os.getcwd()
26
+ Out[2]: '/home/jarrod/src/scientific-python-lectures/intro'
25
27
26
28
List a directory:
27
29
28
30
.. ipython ::
29
31
30
- In [31]: os.listdir(os.curdir)
31
- Out[31]:
32
- ['.index.rst.swo',
33
- '.python_language.rst.swp',
34
- '.view_array.py.swp',
35
- '_static',
36
- '_templates',
37
- 'basic_types.rst',
38
- 'conf.py',
39
- 'control_flow.rst',
40
- 'debugging.rst',
41
- ...
32
+ In [3]: os.listdir(os.curdir)
33
+ Out[3]: ['intro.rst', 'scipy', 'language', 'matplotlib', 'index.rst', 'numpy', 'help']
42
34
43
35
Make a directory:
44
36
45
37
.. ipython ::
46
38
47
- In [32 ]: os.mkdir('junkdir')
39
+ In [4 ]: os.mkdir('junkdir')
48
40
49
- In [33 ]: 'junkdir' in os.listdir(os.curdir)
50
- Out[33 ]: True
41
+ In [5 ]: 'junkdir' in os.listdir(os.curdir)
42
+ Out[5 ]: True
51
43
52
44
Rename the directory:
53
45
54
46
.. ipython ::
55
47
56
- In [36 ]: os.rename('junkdir', 'foodir')
48
+ In [6 ]: os.rename('junkdir', 'foodir')
57
49
58
- In [37 ]: 'junkdir' in os.listdir(os.curdir)
59
- Out[37 ]: False
50
+ In [7 ]: 'junkdir' in os.listdir(os.curdir)
51
+ Out[7 ]: False
60
52
61
- In [38 ]: 'foodir' in os.listdir(os.curdir)
62
- Out[38 ]: True
53
+ In [8 ]: 'foodir' in os.listdir(os.curdir)
54
+ Out[8 ]: True
63
55
64
- In [41 ]: os.rmdir('foodir')
56
+ In [9 ]: os.rmdir('foodir')
65
57
66
- In [42 ]: 'foodir' in os.listdir(os.curdir)
67
- Out[42 ]: False
58
+ In [10 ]: 'foodir' in os.listdir(os.curdir)
59
+ Out[10 ]: False
68
60
69
61
Delete a file:
70
62
71
63
.. ipython ::
72
64
73
- In [44 ]: fp = open('junk.txt', 'w')
65
+ In [11 ]: fp = open('junk.txt', 'w')
74
66
75
- In [45 ]: fp.close()
67
+ In [12 ]: fp.close()
76
68
77
- In [46 ]: 'junk.txt' in os.listdir(os.curdir)
78
- Out[46 ]: True
69
+ In [13 ]: 'junk.txt' in os.listdir(os.curdir)
70
+ Out[13 ]: True
79
71
80
- In [47 ]: os.remove('junk.txt')
72
+ In [14 ]: os.remove('junk.txt')
81
73
82
- In [48 ]: 'junk.txt' in os.listdir(os.curdir)
83
- Out[48 ]: False
74
+ In [15 ]: 'junk.txt' in os.listdir(os.curdir)
75
+ Out[15 ]: False
84
76
85
77
``os.path ``: path manipulations
86
78
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -89,52 +81,50 @@ Delete a file:
89
81
90
82
.. ipython ::
91
83
92
- In [70 ]: fp = open('junk.txt', 'w')
84
+ In [16 ]: fp = open('junk.txt', 'w')
93
85
94
- In [71 ]: fp.close()
86
+ In [17 ]: fp.close()
95
87
96
- In [72 ]: a = os.path.abspath('junk.txt')
88
+ In [18 ]: a = os.path.abspath('junk.txt')
97
89
98
- In [73 ]: a
99
- Out[73 ]: '/Users/cburns /src/scipy2009/scipy_2009_tutorial/source /junk.txt'
90
+ In [19 ]: a
91
+ Out[19 ]: '/home/jarrod /src/scientific-python-lectures/intro /junk.txt'
100
92
101
- In [74]: os.path.split(a)
102
- Out[74]: ('/Users/cburns/src/scipy2009/scipy_2009_tutorial/source',
103
- 'junk.txt')
93
+ In [20]: os.path.split(a)
94
+ Out[20]: ('/home/jarrod/src/scientific-python-lectures/intro', 'junk.txt')
104
95
105
- In [78 ]: os.path.dirname(a)
106
- Out[78 ]: '/Users/cburns /src/scipy2009/scipy_2009_tutorial/source '
96
+ In [21 ]: os.path.dirname(a)
97
+ Out[21 ]: '/home/jarrod /src/scientific-python-lectures/intro '
107
98
108
- In [79 ]: os.path.basename(a)
109
- Out[79 ]: 'junk.txt'
99
+ In [22 ]: os.path.basename(a)
100
+ Out[22 ]: 'junk.txt'
110
101
111
- In [80 ]: os.path.splitext(os.path.basename(a))
112
- Out[80 ]: ('junk', '.txt')
102
+ In [23 ]: os.path.splitext(os.path.basename(a))
103
+ Out[23 ]: ('junk', '.txt')
113
104
114
- In [84 ]: os.path.exists('junk.txt')
115
- Out[84 ]: True
105
+ In [24 ]: os.path.exists('junk.txt')
106
+ Out[24 ]: True
116
107
117
- In [86 ]: os.path.isfile('junk.txt')
118
- Out[86 ]: True
108
+ In [25 ]: os.path.isfile('junk.txt')
109
+ Out[25 ]: True
119
110
120
- In [87 ]: os.path.isdir('junk.txt')
121
- Out[87 ]: False
111
+ In [26 ]: os.path.isdir('junk.txt')
112
+ Out[26 ]: False
122
113
123
- In [88 ]: os.path.expanduser('~/local')
124
- Out[88 ]: '/Users/cburns /local'
114
+ In [27 ]: os.path.expanduser('~/local')
115
+ Out[27 ]: '/home/jarrod /local'
125
116
126
- In [92 ]: os.path.join(os.path.expanduser('~'), 'local', 'bin')
127
- Out[92 ]: '/Users/cburns /local/bin'
117
+ In [28 ]: os.path.join(os.path.expanduser('~'), 'local', 'bin')
118
+ Out[28 ]: '/home/jarrod /local/bin'
128
119
129
120
Running an external command
130
121
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
131
122
132
123
.. ipython ::
133
124
134
- In [8]: os.system('ls')
135
- basic_types.rst demo.py functions.rst python_language.rst standard_library.rst
136
- control_flow.rst exceptions.rst io.rst python-logo.png
137
- demo2.py first_steps.rst oop.rst reusing_code.rst
125
+ In [29]: os.system('ls')
126
+ help index.rst intro.rst junk.txt language matplotlib numpy scipy
127
+ Out[29]: 0
138
128
139
129
.. note :: Alternative to ``os.system``
140
130
@@ -145,20 +135,17 @@ Running an external command
145
135
.. ipython ::
146
136
:verbatim:
147
137
148
- In [20 ]: import sh
149
- In [20 ]: com = sh.ls()
138
+ In [30 ]: import sh
139
+ In [31 ]: com = sh.ls()
150
140
151
- In [21 ]: print(com)
141
+ In [32 ]: print(com)
152
142
basic_types.rst exceptions.rst oop.rst standard_library.rst
153
143
control_flow.rst first_steps.rst python_language.rst
154
144
demo2.py functions.rst python-logo.png
155
145
demo.py io.rst reusing_code.rst
156
146
157
- In [22]: print(com.exit_code)
158
- 0
159
- In [23]: type(com)
160
- Out[23]: sh.RunningCommand
161
-
147
+ In [33]: type(com)
148
+ Out[33]: str
162
149
163
150
Walking a directory
164
151
~~~~~~~~~~~~~~~~~~~~
@@ -172,11 +159,11 @@ Walking a directory
172
159
....: print(os.path.abspath(fp))
173
160
....:
174
161
....:
175
- /Users/cburns /src/scipy2009/scipy_2009_tutorial/source/.index. rst.swo
176
- /Users/cburns /src/scipy2009/scipy_2009_tutorial/source/.view_array.py.swp
177
- /Users/cburns /src/scipy2009/scipy_2009_tutorial/source/basic_types .rst
178
- /Users/cburns /src/scipy2009/scipy_2009_tutorial/source/conf.py
179
- /Users/cburns /src/scipy2009/scipy_2009_tutorial/source/control_flow .rst
162
+ /home/jarrod /src/scientific-python-lectures/intro/language/basic_types. rst
163
+ /home/jarrod /src/scientific-python-lectures/intro/language/control_flow.rst
164
+ /home/jarrod /src/scientific-python-lectures/intro/language/python_language .rst
165
+ /home/jarrod /src/scientific-python-lectures/intro/language/reusing_code.rst
166
+ /home/jarrod /src/scientific-python-lectures/intro/language/standard_library .rst
180
167
...
181
168
182
169
Environment variables:
@@ -185,35 +172,12 @@ Environment variables:
185
172
.. ipython ::
186
173
:verbatim:
187
174
188
- In [9]: import os
189
-
190
- In [11]: os.environ.keys()
191
- Out[11]:
192
- ['_',
193
- 'FSLDIR',
194
- 'TERM_PROGRAM_VERSION',
195
- 'FSLREMOTECALL',
196
- 'USER',
197
- 'HOME',
198
- 'PATH',
199
- 'PS1',
200
- 'SHELL',
201
- 'EDITOR',
202
- 'WORKON_HOME',
203
- 'PYTHONPATH',
204
- ...
205
-
206
- In [12]: os.environ['PYTHONPATH']
207
- Out[12]: '.:/Users/cburns/src/utils:/Users/cburns/src/nitools:
208
- /Users/cburns/local/lib/python2.5/site-packages/:
209
- /usr/local/lib/python2.5/site-packages/:
210
- /Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5'
211
-
212
- In [16]: os.getenv('PYTHONPATH')
213
- Out[16]: '.:/Users/cburns/src/utils:/Users/cburns/src/nitools:
214
- /Users/cburns/local/lib/python2.5/site-packages/:
215
- /usr/local/lib/python2.5/site-packages/:
216
- /Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5'
175
+ In [32]: os.environ.keys()
176
+ Out[32]: KeysView(environ({'SHELL': '/bin/bash', 'COLORTERM': 'truecolor', ...}))
177
+
178
+
179
+ In [34]: os.environ['SHELL']
180
+ Out[34]: '/bin/bash'
217
181
218
182
219
183
``shutil ``: high-level file operations
@@ -234,12 +198,10 @@ Find all files ending in ``.txt``:
234
198
235
199
.. ipython ::
236
200
237
- In [18]: import glob
238
-
239
- In [19]: glob.glob('*.txt')
240
- Out[19]: ['holy_grail.txt', 'junk.txt', 'newfile.txt']
241
-
201
+ In [36]: import glob
242
202
203
+ In [37]: glob.glob('*.txt')
204
+ Out[37]: ['junk.txt']
243
205
244
206
``sys `` module: system-specific information
245
207
--------------------------------------------
@@ -248,41 +210,41 @@ System-specific information related to the Python interpreter.
248
210
249
211
* Which version of python are you running and where is it installed:
250
212
251
- .. ipython ::
213
+ .. ipython ::
252
214
253
- In [117]: sys.platform
254
- Out[117]: 'darwin'
255
215
256
- In [118]: sys.version
257
- Out[118]: '2.5.2 (r252:60911, Feb 22 2008, 07:57:53) \n
258
- [GCC 4.0.1 (Apple Computer, Inc. build 5363)]'
216
+ In [39]: import sys
259
217
260
- In [119 ]: sys.prefix
261
- Out[119 ]: '/Library/Frameworks/Python.framework/Versions/2.5 '
218
+ In [40 ]: sys.platform
219
+ Out[40 ]: 'linux '
262
220
263
- * List of command line arguments passed to a Python script:
221
+ In [41]: sys.version
222
+ Out[41]: '3.11.8 (main, Feb 28 2024, 00:00:00) [GCC 13.2.1 20231011 (Red Hat 13.2.1-4)]'
264
223
265
- .. ipython ::
224
+ In [42]: sys.prefix
225
+ Out[42]: '/home/jarrod/.venv/nx'
266
226
267
- In [100]: sys.argv
268
- Out[100]: ['/Users/cburns/local/bin/ipython']
227
+ * List of command line arguments passed to a Python script:
228
+
229
+ .. ipython ::
269
230
231
+ In [43]: sys.argv
232
+ Out[43]: ['/home/jarrod/.venv/nx/bin/ipython']
270
233
271
234
``sys.path `` is a list of strings that specifies the search path for
272
235
modules. Initialized from PYTHONPATH:
273
236
274
237
.. ipython ::
275
238
276
- In [121]: sys.path
277
- Out[121]:
278
- ['',
279
- '/Users/cburns/local/bin',
280
- '/Users/cburns/local/lib/python2.5/site-packages/grin-1.1-py2.5.egg',
281
- '/Users/cburns/local/lib/python2.5/site-packages/argparse-0.8.0-py2.5.egg',
282
- '/Users/cburns/local/lib/python2.5/site-packages/urwid-0.9.7.1-py2.5.egg',
283
- '/Users/cburns/local/lib/python2.5/site-packages/yolk-0.4.1-py2.5.egg',
284
- '/Users/cburns/local/lib/python2.5/site-packages/virtualenv-1.2-py2.5.egg',
285
- ...
239
+ In [44]: sys.path
240
+ Out[44]:
241
+ ['/home/jarrod/.venv/nx/bin',
242
+ '/usr/lib64/python311.zip',
243
+ '/usr/lib64/python3.11',
244
+ '/usr/lib64/python3.11/lib-dynload',
245
+ '',
246
+ '/home/jarrod/.venv/nx/lib64/python3.11/site-packages',
247
+ '/home/jarrod/.venv/nx/lib/python3.11/site-packages']
286
248
287
249
``pickle ``: easy persistence
288
250
-------------------------------
@@ -291,9 +253,9 @@ Useful to store arbitrary objects to a file. Not safe or fast!
291
253
292
254
.. ipython ::
293
255
294
- In [1 ]: import pickle
256
+ In [45 ]: import pickle
295
257
296
- In [2 ]: l = [1, None, 'Stan']
258
+ In [46 ]: l = [1, None, 'Stan']
297
259
298
260
In [3]: with open('test.pkl', 'wb') as file:
299
261
...: pickle.dump(l, file)
@@ -303,7 +265,8 @@ Useful to store arbitrary objects to a file. Not safe or fast!
303
265
...: out = pickle.load(file)
304
266
...:
305
267
306
- In [5]: out
268
+ In [49]: out
269
+ Out[49]: [1, None, 'Stan']
307
270
308
271
309
272
.. topic :: Exercise
0 commit comments