1

00:00:00,210  -->  00:00:06,600
Welcome to lesson 31 which roughly covers pages 181 to 185 of the automate the boring stuff with Python

2

00:00:06,600  -->  00:00:11,760
textbook now that we know about file names and file paths we can start writing strings to files that

3

00:00:11,760  -->  00:00:12,530
we create.

4

00:00:12,530  -->  00:00:15,260
This is a great way to save information to the hard drive.

5

00:00:15,300  -->  00:00:20,300
You can then later read these files or any file on the hard drive back into your program.

6

00:00:20,320  -->  00:00:24,780
This is just like saving and opening documents in an app with type of file that will be reading and

7

00:00:24,780  -->  00:00:30,480
writing are called text or plain text files plain text files only contain basic text characters and

8

00:00:30,480  -->  00:00:37,050
they don't include any information about font or size or color text files usually have the text file

9

00:00:37,050  -->  00:00:38,060
extension.

10

00:00:38,100  -->  00:00:45,630
Python scripts are an example of plain text files except they also have the dot why file extension plain

11

00:00:45,630  -->  00:00:51,060
text files can be opened with programs like Windows Notepad or tense text edit application.

12

00:00:51,060  -->  00:00:57,420
For example I'll just open up one of my plain text python files with Notepad

13

00:01:02,270  -->  00:01:05,330
and these files just have the plain text information.

14

00:01:05,340  -->  00:01:11,040
They don't have anything else like a word processor would have with fonts or centering text or anything

15

00:01:11,040  -->  00:01:11,570
else like that.

16

00:01:11,580  -->  00:01:16,160
It's just text just like a python string is just text.

17

00:01:16,170  -->  00:01:21,690
Now the other type of files besides plain text files are called binary files and these are every other

18

00:01:21,690  -->  00:01:23,160
type of file.

19

00:01:23,250  -->  00:01:28,800
Word processing documents PTF images spreadsheets executable programs.

20

00:01:28,950  -->  00:01:35,040
If you open a binary file in Notepad like I'll open up windows calculator program calc.

21

00:01:35,070  -->  00:01:41,370
You see you're going to find that it's going to be impossible to understand.

22

00:01:41,710  -->  00:01:45,540
Since every different type of binary file has to be handled in its own way.

23

00:01:45,540  -->  00:01:50,110
This book really won't go into reading and writing raw binary files directly.

24

00:01:50,130  -->  00:01:55,110
Most of the time there are modules to handle these binary file formats for you anyway and will be going

25

00:01:55,110  -->  00:01:58,510
into them in future lessons in this course.

26

00:01:58,540  -->  00:02:01,380
There are three steps to reading or writing files in Python.

27

00:02:01,380  -->  00:02:03,950
First you're going to call the open function.

28

00:02:03,960  -->  00:02:07,340
You'll pass it a file name that you'd like to open.

29

00:02:07,530  -->  00:02:09,090
So I would like to open

30

00:02:11,640  -->  00:02:14,000
users slash now.

31

00:02:14,490  -->  00:02:23,840
Hello 0.60 fact let's go ahead and use Notepad to create this file.

32

00:02:28,020  -->  00:02:33,120
So an O is 10 you can use text at it or some other plain text file editor program.

33

00:02:33,120  -->  00:02:38,970
You can create this file right here and then save it to your user accounts home folder.

34

00:02:38,970  -->  00:02:46,290
On Windows this is going to be something like C slash users slash Al and I'll just save this as whole

35

00:02:46,320  -->  00:02:47,300
0.60.

36

00:02:47,310  -->  00:02:53,700
If you're on OS 10 your users folder is going to be slash users slash whatever your account name is

37

00:02:53,970  -->  00:02:56,350
and then you can save haloed at 60 there.

38

00:02:56,940  -->  00:03:01,080
So this function call will open the file in plain text read mode.

39

00:03:01,080  -->  00:03:04,530
We'll just call this read mode for short when the file is open read mode.

40

00:03:04,560  -->  00:03:06,960
Python lets you only read data from the file.

41

00:03:06,960  -->  00:03:09,390
You can't write or modify it in any way.

42

00:03:09,520  -->  00:03:12,620
Reimold is the default mode for files you open in Python.

43

00:03:12,720  -->  00:03:17,230
With this open function we call the Open returns a file object.

44

00:03:17,370  -->  00:03:19,660
So we are going to save that too.

45

00:03:19,920  -->  00:03:21,120
Hello file.

46

00:03:21,120  -->  00:03:29,190
So the file data type has several methods including one is the read method and this will return a string

47

00:03:29,460  -->  00:03:35,190
of the entire plain text files contents plus just copy and paste this and run it in the interactive

48

00:03:35,190  -->  00:03:35,640
shell

49

00:03:38,400  -->  00:03:43,980
file to read and that returns the contents of that file that we just typed.

50

00:03:43,980  -->  00:03:49,770
You notice that the new line character is represented as slash in and after we're done reading the file

51

00:03:49,770  -->  00:03:55,380
contents will call the close method to close that file.

52

00:03:55,380  -->  00:03:59,670
Now we can only read through the file contents once if we want to read it again.

53

00:03:59,700  -->  00:04:09,560
We'll have to call open again so we should probably save this to a variable save content.

54

00:04:12,690  -->  00:04:18,670
Alternatively there is also a read lines method for file objects.

55

00:04:18,720  -->  00:04:25,920
This will return all of the lines as strings inside if a list is just a different way of getting the

56

00:04:25,920  -->  00:04:31,980
file contents in a structure that you want realigns will return a list of strings whereas read will

57

00:04:31,980  -->  00:04:34,530
simply just return a single string

58

00:04:36,940  -->  00:04:39,030
.

59

00:04:39,570  -->  00:04:45,150
Python allows you to write content to files in a similar way that how the print function writes strings

60

00:04:45,150  -->  00:04:46,290
to the screen.

61

00:04:46,290  -->  00:04:50,580
You can't write to a file that you've opened in read mode though instead you'll have to open it in write

62

00:04:50,580  -->  00:04:56,580
mode or append mode so right mood will overwrite an existing file and start from scratch with a blank

63

00:04:56,610  -->  00:05:01,760
text file just like when you overwrite variables value with a new value to open in write mode.

64

00:05:01,860  -->  00:05:07,460
Pass the string W to the open function as the second argument append mode.

65

00:05:07,470  -->  00:05:12,210
On the other hand will append the text that you're writing to the end of an existing file so you can

66

00:05:12,210  -->  00:05:18,030
think of this is like a pending to a list in a variable rather than overwriting the variable altogether

67

00:05:18,560  -->  00:05:24,240
to open a pen mode you'll just pass a lower case a as the second argument to the open function.

68

00:05:24,240  -->  00:05:30,600
Now in both cases if this file doesn't already exist Python will just create a new blank text file for

69

00:05:30,600  -->  00:05:31,850
you to write to.

70

00:05:31,900  -->  00:05:36,860
I'm just going to create a file hello to and open it and write mode.

71

00:05:37,740  -->  00:05:44,160
And in order to write strings to this file or call the right method and pass it a string that I want

72

00:05:44,160  -->  00:05:46,350
to write to it

73

00:05:49,650  -->  00:05:55,420
and write will return the integer of how many bytes of how many characters it wrote to it.

74

00:05:55,470  -->  00:06:02,550
And generally you'll just ignore this information so I can call this multiple times and finally close

75

00:06:02,550  -->  00:06:03,560
that file.

76

00:06:03,870  -->  00:06:10,650
So I'll use Notepad to go ahead and open this hello to that text file.

77

00:06:10,710  -->  00:06:15,600
And you'll notice that the right method doesn't automatically add a newline character to the end of

78

00:06:15,600  -->  00:06:20,680
each string that we passed it the way the print function does when it prints things to string prints

79

00:06:20,700  -->  00:06:22,630
things to the screen.

80

00:06:22,650  -->  00:06:26,940
So if we want new lines we're going to have to add them ourselves to the end of the strings that we

81

00:06:26,940  -->  00:06:31,050
passed like this slash in for the new line character.

82

00:06:32,100  -->  00:06:35,020
Let's just have another example.

83

00:06:35,370  -->  00:06:38,730
Let's say they can file.

84

00:06:40,050  -->  00:06:48,740
And let's just say and at TXI he will open this in write mode we'll just write the string.

85

00:06:48,900  -->  00:06:54,910
Bacon is not a vegetable.

86

00:06:55,640  -->  00:06:57,140
And then we'll close that.

87

00:06:57,150  -->  00:07:02,430
Now this is a relative file path so you might be wondering where on my hard drive is this actually Wolf

88

00:07:02,460  -->  00:07:04,060
you remember from last lesson.

89

00:07:04,210  -->  00:07:11,510
You can import the OS module and the OS module has a function called Get current working directory.

90

00:07:11,820  -->  00:07:18,870
So Python is just going to use this file path relative to the current working directory.

91

00:07:19,230  -->  00:07:23,300
So here is where we're going to find that vacante XTi file.

92

00:07:23,370  -->  00:07:34,010
Print this out so it's easier to copy to the clipboard control see the copy file open and paste it here

93

00:07:34,850  -->  00:07:40,970
and see here in this folder is where that big and text file is.

94

00:07:41,400  -->  00:07:47,940
And just as an example I'll open this file now in append mode so that it doesn't overwrite the contents

95

00:07:47,940  -->  00:07:49,530
that are already there.

96

00:07:50,220  -->  00:07:57,540
And I'll just write some more text I'll add a new line maybe a couple new lines and I'll just add some

97

00:07:57,540  -->  00:07:58,150
text.

98

00:07:58,170  -->  00:08:00,390
Bacon is delicious

99

00:08:03,190  -->  00:08:06,480
.

100

00:08:07,140  -->  00:08:13,590
I'll just have to re-open this file and you see there were two new line characters added along with

101

00:08:13,740  -->  00:08:22,010
bacon is delicious and then append mode the original contents of the file weren't erased.

102

00:08:22,080  -->  00:08:26,730
Writing and reading text files is a good way to store a single long string but if you want to store

103

00:08:26,730  -->  00:08:32,340
variables that have lists and dictionaries and other complex data structures you can see variables in

104

00:08:32,340  -->  00:08:37,490
your Python programs to binary shell files using the shell module.

105

00:08:37,620  -->  00:08:46,200
So I'll give you an example let's just import the shell module and then we'll call shelved up open and

106

00:08:46,200  -->  00:08:52,890
we'll just say this file is going to be called my data and this will return a shell file object to store

107

00:08:52,890  -->  00:08:55,370
that in a variable called Shell file.

108

00:08:55,370  -->  00:09:00,510
You can make changes to the shell value as if it were a dictionary and when you're done just cold call

109

00:09:00,510  -->  00:09:03,340
close on the shelf value.

110

00:09:03,390  -->  00:09:05,430
So for example let's say shelf.

111

00:09:05,490  -->  00:09:14,000
I'll have a key called cats and in this I'll just store a list of my cat names.

112

00:09:14,290  -->  00:09:14,850
Sophie

113

00:09:18,240  -->  00:09:23,730
Simon fantail Cleo.

114

00:09:24,130  -->  00:09:31,080
I actually don't have this many cats and they are called File clothes and then later when I run this

115

00:09:31,080  -->  00:09:39,840
program my program again in the future I could just have a code that reopens that shelf file and I can

116

00:09:39,840  -->  00:09:43,020
grab the value just like a dictionary.

117

00:09:43,020  -->  00:09:48,900
In fact if you think about it variables inside of Python programs are kind of like key value pairs in

118

00:09:48,900  -->  00:09:49,800
a dictionary.

119

00:09:49,890  -->  00:09:54,910
I had a variable called cats and I saved this string to it.

120

00:09:54,930  -->  00:10:01,650
Consider this cat's variable to sort of be the key in this list to be the value.

121

00:10:01,740  -->  00:10:04,250
So that's why it has a dictionary like structure for this.

122

00:10:04,270  -->  00:10:06,250
And see here we just have a keys.

123

00:10:06,300  -->  00:10:12,690
The string cats and then the value of whatever we want to store the benefit of using the shell module

124

00:10:12,900  -->  00:10:18,600
is that you can store things like list and dictionaries and non-text data to a file and then reopen

125

00:10:18,600  -->  00:10:26,010
them in the future with your python program.

126

00:10:26,070  -->  00:10:29,860
Now you might be wondering what does this actually look like on your hard drive.

127

00:10:29,990  -->  00:10:35,940
So let's go back to the current working directory folder where I originally had this.

128

00:10:35,940  -->  00:10:37,790
My data.

129

00:10:38,000  -->  00:10:39,830
I'll just open up the file explorer.

130

00:10:40,200  -->  00:10:46,470
Here you see on Windows This actually creates three new files where all this data is stored my data

131

00:10:46,470  -->  00:10:50,610
that are my data that dat my data up back on always 10 Linux.

132

00:10:50,610  -->  00:10:53,850
I believe that there is only one file that's created my data.

133

00:10:53,960  -->  00:11:00,620
D-B but these are the files where this information for the shelf is all stored.

134

00:11:00,720  -->  00:11:03,810
And these are binary files so if you open this in Notepad

135

00:11:12,340  -->  00:11:15,770
there would also have this weird sort of binary file format.

136

00:11:15,780  -->  00:11:22,050
You can kind of see the data here and really you could just open this file as a plain text file to read

137

00:11:22,050  -->  00:11:24,120
this as a string but it would be hard to figure out.

138

00:11:24,140  -->  00:11:31,310
Oh I guess this means the beginning of the list value and this means like that comma insert.

139

00:11:31,320  -->  00:11:35,880
It's better just to let the shelf marshal handle all these details for you.

140

00:11:36,480  -->  00:11:40,480
So Shell file objects are very similar to dictionaries.

141

00:11:40,500  -->  00:11:47,280
In fact they even have PE keys and values method that will return a list like values for all the keys

142

00:11:47,280  -->  00:11:51,130
and values inside them so let's just convert this to an actual list.

143

00:11:51,270  -->  00:11:56,070
So here are all the keys inside that shelf module are inside that shell file

144

00:11:57,320  -->  00:12:00,840
.

145

00:12:00,950  -->  00:12:03,660
We can also call its values method as well.

146

00:12:03,930  -->  00:12:11,610
So if you just need to store plain text you can open up a program using the open function and then just

147

00:12:11,670  -->  00:12:18,360
use write or read or read lines to write and read the text to these files as strings.

148

00:12:18,460  -->  00:12:24,240
But if you want to store complex data like lists and dictionaries or if you just want to store strings

149

00:12:24,390  -->  00:12:30,150
but use this sort of dictionary format you can use a shell file in the shell module.

150

00:12:30,630  -->  00:12:31,500
To recap.

151

00:12:31,500  -->  00:12:35,580
Open will return a file object which has reading and writing related methods.

152

00:12:35,580  -->  00:12:40,740
You can pass the string are or more commonly nothing as the second argument to open to open the file

153

00:12:40,740  -->  00:12:47,400
in read mode or pass the string W for right mode or the string a for append mode opening a non-existent

154

00:12:47,400  -->  00:12:50,620
file in right where we're pen mode will create that file.

155

00:12:50,640  -->  00:12:53,760
It'll just create a blank file that you'll start writing to.

156

00:12:54,180  -->  00:12:59,520
You can call the read or write methods on a file object to read the contents of the file or write a

157

00:12:59,520  -->  00:13:00,600
string to that file.

158

00:13:00,750  -->  00:13:06,890
Or you could call the read lines method to return a list of strings of the files content then call close

159

00:13:06,900  -->  00:13:08,370
after you're done with the file.

160

00:13:08,580  -->  00:13:15,060
Meanwhile there is also a shell module that Python can use to store Python data structures like lists

161

00:13:15,060  -->  00:13:22,140
and dictionaries into a binary file and you can get a shelf a file object by calling shell open to return

162

00:13:22,140  -->  00:13:22,260
it.

163

00:13:22,260  -->  00:13:24,250
This dictionary like shelf value
