text
stringlengths
1
81
start
float64
0
10.1k
duration
float64
0
24.9
necessarily a big deal but sometimes it
842.82
3.36
can create problems files could get
844.62
3.48
corrupted or accidentally deleted or the
846.18
3.3
liked depending on what happens in your
848.1
3.12
code so it turns out that you don't
849.48
4.56
strictly need to call close on the file
851.22
4.679
yourself if you take another approach
854.04
5.7
instead more pythonic when manipulating
855.899
7.081
files is to do this to introduce this
859.74
6.06
other keyword called quite simply with
862.98
5.52
that allows you to specify that in this
865.8
5.039
context I want you to open and
868.5
5.1
automatically close some file so how do
870.839
4.921
we use with it simply looks like this
873.6
4.14
let me go back to my code here I've
875.76
4.199
gotten rid of the close line and I'm now
877.74
4.14
just going to say this instead instead
879.959
4.56
of saying file equals open I'm going to
881.88
6
say with open then the same arguments as
884.519
5.521
before and somewhat curiously I'm going
887.88
3.84
to put the variable at the end of the
890.04
3.72
line why that's just the way this is
891.72
4.44
done you say with you call the function
893.76
4.74
in question and then you say as and
896.16
4.02
specify the name of the variable that
898.5
3.54
should be assigned find the return value
900.18
3.959
of open then I'm going to go ahead and
902.04
4.5
indent the line underneath so that the
904.139
4.44
line of code that's writing the name is
906.54
4.2
now in the context of this with
908.579
4.32
statement which just ensures that
910.74
4.5
automatically if I had more code in this
912.899
5.221
file down below no longer indented the
915.24
4.98
file would be automatically closed as
918.12
4.44
soon as line 4 is done executing so it
920.22
3.72
doesn't change what has just happened
922.56
3.18
but it does automate the process of at
923.94
3.48
least closing things for us just to
925.74
3
ensure I don't forget and so that
927.42
4.08
something doesn't go wrong
928.74
5.58
but suppose now that I wanted to read
931.5
4.8
these names from the file all I've done
934.32
3.6
thus far is write code that writes names
936.3
3.659
to the file but let's assume now that we
937.92
3.96
have all of these names in the file and
939.959
4.081
heck let's go ahead and add one more let
941.88
3.78
me go ahead and run this one more time
944.04
4.32
python of names.pi and let's add in
945.66
5.099
Draco to the mix so now that we have all
948.36
4.56
four of these names here how might we
950.759
4.5
want to read them back well let me
952.92
4.56
propose that we go into names.pi now or
955.259
3.181
we could create another program
957.48
2.76
altogether but I'm going to keep reusing
958.44
3.72
the same name just to keep us focused on
960.24
3.779
this and now I'm going to write code
962.16
4.919
that reads an existing file with
964.019
6.18
Hermione Harry Ron and Draco together
967.079
5.341
and how do I do this well it's similar
970.199
3.961
in spirit I'm going to start this time
972.42
4.44
with with open and then the first
974.16
3.9
argument is going to be the name of the
976.86
3.599
file that I want to open as before and
978.06
4.26
I'm going to open it this time in read
980.459
4.201
mode quote unquote R and to read a file
982.32
5.519
just means to load it not to save it and
984.66
5.64
I'm going to name the return value file
987.839
4.141
and now I'm going to do this yes and
990.3
3
there's a number of ways I can do this
991.98
3.24
but one way to read all of the lines
993.3
4.14
from the file at once would be this let
995.22
4.44
me declare a variable called lines let
997.44
4.44
me access that file and call a function
999.66
4.02
or a method that comes with it called
1,001.88
3.66
read lines so if you read the
1,003.68
4.079
documentation on file i o and python
1,005.54
4.5
you'll see that open files come with a
1,007.759
4.2
special method whose purpose in life is
1,010.04
4.38
to read all the lines from the file and
1,011.959
5.221
return them to me as a list so what this
1,014.42
4.8
line 2 is doing is it's reading all of
1,017.18
4.32
the lines from that file storing them in
1,019.22
5.099
a variable called lines now suppose I
1,021.5
4.319
want to iterate over all of those lines
1,024.319
4.02
and print out each of those names for
1,025.819
4.921
line in lines this is just a standard
1,028.339
6.061
for Loop in Python lines as a list line
1,030.74
4.92
is the variable that will be
1,034.4
3.059
automatically be set to each of those
1,035.66
3.539
lines let me go ahead and print out
1,037.459
3.96
something like oh hello
1,039.199
5.1
uh comma and then I'll print out the
1,041.419
4.201
line itself
1,044.299
3.301
all right so let me go to my terminal
1,045.62
5.64
window run python of names.pi now I have
1,047.6
5.939
not deleted names.txt so it still
1,051.26
3.96
contains all four of those names and hit
1,053.539
5.701
enter and okay it's not bad but it's a
1,055.22
6
little ugly here
1,059.24
4.799
what's going on when I ran names.pi it's
1,061.22
4.74