COMP 364: Computer Tools for Life Sciences
Python programming: File IO Christopher J.F. Cameron and Carlos G. Oliver
1 / 22
COMP 364: Computer Tools for Life Sciences Python programming: File - - PowerPoint PPT Presentation
COMP 364: Computer Tools for Life Sciences Python programming: File IO Christopher J.F. Cameron and Carlos G. Oliver 1 / 22 Reading/writing files in Python Pythons built-in open() function returns a file-stream object most commonly used
1 / 22
1
2
3
4 5
6
7
8
9
2 / 22
3 / 22
4 / 22
5 / 22
◮ or bytes object in binary mode
◮ in number of characters
◮ the entire contents of the file will be read and returned 1
2
3
6 / 22
◮ if the file doesn’t end with ‘\n’
1
2
3
4
5
7 / 22
1
2
3
4
5
6
7
8
9
10
8 / 22
1
2
3 4
5
6 7
9 / 22
1
2
3
4 5
6
7
8
9
10
10 / 22
◮ bytes in binary mode 1
2
3
4
5
6
7
8
9
11 / 22
◮ 0 measures from the beginning of the file ◮ 1 uses the current file position ◮ 2 uses the end of the file as the reference point ◮ defaults to 0
◮ binary files allow other from what options 1
2
3
12 / 22
◮ along with binary options (i.e., ab) 1
2 3
4
5
6
7
13 / 22
◮ which takes a string like ’123’ ◮ returns its numeric value 123
14 / 22
1
2 3
4
5
15 / 22
16 / 22
1
2 3
4
1
2 3
4
5
17 / 22
◮ comments not typically used
18 / 22
19 / 22
20 / 22
21 / 22
1
2 3
4
5
6
7
8
9
10
11
12
22 / 22