Study Drill 6:
If you want to open a file from within the python shell you don’t need
argv. argv you only need if you want to run a script and give some parameter to it.
So, go to your terminal an open up a python shell:
Then, inside the Python shell you can open the file
You see something like this:
<_io.TextIOWrapper name='ex15_sample.txt' mode='r' encoding='UTF-8'>
Study Drill 7:
After you opened the file you can do some work with it. To do that you need to assign your open function to a variable. So instead of
open("ex15_sample.txt") you have to do something like this
filename = open("ex15_sample.txt")
Whit that you have a variable you can work with.
To read the content of the file
open("ex15_sample.txt") you have to write:
to close it you have to write:
The whole code:
Python 3.6.7 (default, Oct 25 2018, 09:16:13)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> filename = open("ex15_sample.txt")
'This is stuff I typed into a file.\nIt is really cool stuff.\nLots and lots of fun to have in here.\n'
The difference between
close() is that
open() is a function and
close() is a method. The first one you call first, like
open("sample.txt") the second one you call after, like
filename.close(). You don’t have to bother with the difference, you will learn later what it means, the only important thing for you for now is to remember that you have to call them differently.