text stringlengths 1 81 | start float64 0 10.1k | duration float64 0 24.9 |
|---|---|---|
of like double clicking on an icon on | 392.699 | 4.261 |
your Mac or PC but it's a programmer's | 394.38 | 3.9 |
technique because it's going to allow | 396.96 | 3.299 |
you to specify exactly what you want to | 398.28 | 5.1 |
read from or write to that file formally | 400.259 | 5.461 |
its documentation is here and you'll see | 403.38 | 4.02 |
that its usage is relatively | 405.72 | 3.06 |
straightforward it minimally just | 407.4 | 3 |
requires the name of the file that we | 408.78 | 4.02 |
want to open and optionally how we want | 410.4 | 4.68 |
to open it so let me go back to vs code | 412.8 | 4.38 |
here and let me propose now that I do | 415.08 | 4.26 |
this I'm going to go ahead and call this | 417.18 | 4.5 |
function called open passing in an | 419.34 | 4.919 |
argument for names.txt which is the name | 421.68 | 4.919 |
of the file I would like to store all of | 424.259 | 4.021 |
these names in I could call it anything | 426.599 | 3.241 |
I want but because it's going to be just | 428.28 | 3.56 |
text it's conventional to call it | 429.84 | 4.74 |
something.txt but I'm also going to tell | 431.84 | 5.079 |
the open function that I plan to write | 434.58 | 4.92 |
to this file so as a second argument to | 436.919 | 4.201 |
open I'm going to put literally quote | 439.5 | 4.319 |
unquote W for right and that's going to | 441.12 | 4.859 |
tell open to open the file in a way | 443.819 | 3.841 |
that's going to allow me to change the | 445.979 | 3.06 |
contents and better yet if it doesn't | 447.66 | 3.42 |
even exist yet it's going to create the | 449.039 | 4.801 |
file for me now open returns what's | 451.08 | 5.339 |
called a file handle a special value | 453.84 | 4.32 |
that allows me to access that file | 456.419 | 3.361 |
subsequently so I'm going to go ahead | 458.16 | 3.479 |
and sign it equal to a variable like | 459.78 | 4.38 |
file and now I'm going to go ahead and | 461.639 | 5.161 |
quite simply write this person's name to | 464.16 | 4.86 |
that file so I'm going to literally type | 466.8 | 5.88 |
file which is the variable a linking to | 469.02 | 6.42 |
that file dot write which is a function | 472.68 | 4.56 |
otherwise known as a method that comes | 475.44 | 4.14 |
with open files that allows me to write | 477.24 | 5.04 |
that name to the file and then lastly | 479.58 | 4.2 |
I'm going to quite simply going to go | 482.28 | 3.359 |
ahead and say file Dot close which will | 483.78 | 4.68 |
close and effectively save the file so | 485.639 | 4.201 |
these three lines of code here are | 488.46 | 2.82 |
essentially the programmers equivalent | 489.84 | 3.18 |
to like double clicking an icon on your | 491.28 | 3.84 |
Mac or PC making some changes in | 493.02 | 3.84 |
Microsoft Word or some other program and | 495.12 | 3.72 |
going to file save we're doing that all | 496.86 | 4.32 |
in code with just these three lines here | 498.84 | 5.699 |
well let's see now how this works let me | 501.18 | 7.859 |
go ahead now and run python of names.pi | 504.539 | 7.56 |
and enter let's type in a name I'll type | 509.039 | 4.8 |
in Hermione | 512.099 | 5.161 |
enter all right where did she end up | 513.839 | 5.82 |
well let me go ahead now and type code | 517.26 | 5.519 |
of names.txt which is a file that | 519.659 | 5.521 |
happens now to exist because I opened it | 522.779 | 4.441 |
in right mode and if I open this in a | 525.18 | 4.92 |
tab we'll see there's Hermione well | 527.22 | 4.92 |
let's go ahead and run names.pi once | 530.1 | 3.72 |
more I'm going to go ahead and run | 532.14 | 5.1 |
python of names.pi enter and this time | 533.82 | 5.639 |
I'll type in Harry let me go ahead and | 537.24 | 3.96 |
run it one more time and this time I'll | 539.459 | 4.5 |
type in Ron and now let me go up to | 541.2 | 5.579 |
names.text where hopefully I'll see all | 543.959 | 4.56 |
three of them here | 546.779 | 5.521 |
but no I've just actually seen Ron | 548.519 | 6.361 |
What might explain what happened to | 552.3 | 4.5 |
Hermione and Harry even though I'm | 554.88 | 3.84 |
pretty sure I ran the program three | 556.8 | 3.719 |
times and I definitely wrote the code | 558.72 | 5.4 |
that writes their name to that file | 560.519 | 6.241 |
what's going on here do you think I | 564.12 | 4.38 |
think because we're not appending them | 566.76 | 4.56 |
we should append the names since we are | 568.5 | 5.399 |
writing directly it is erasing the old | 571.32 | 4.98 |
content and it is replacing with the | 573.899 | 4.861 |
last uh | 576.3 | 4.5 |
set of characters that we mentioned | 578.76 | 4.86 |
exactly unfortunately quote unquote W is | 580.8 | 4.26 |
a little dangerous not only will it | 583.62 | 3.24 |
create the file for you it will also | 585.06 | 4.26 |
recreate the file for you every time you | 586.86 | 4.2 |
open the file in that mode so if you | 589.32 | 3.48 |
open the file once and write Hermione | 591.06 | 3.719 |
that worked just fine as we saw but if | 592.8 | 3.42 |
you do it again for Harry if you do it | 594.779 | 3.481 |
again for Ron the code is working but | 596.22 | 3.78 |
each time it's opening the file and | 598.26 | 4.319 |
recreating it with brand new contents so | 600 | 4.32 |
we had one version with Hermione one | 602.579 | 3.301 |
version with Harry and one final version | 604.32 | 3.84 |
with Ron but ideally I think we probably | 605.88 | 4.86 |
want to be appending as bashal says each | 608.16 | 4.44 |
of those names to the file not just | 610.74 | 4.14 |
clobbering that is overwriting the file | 612.6 | 4.08 |
each time so how can I do this it's | 614.88 | 4.26 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.