-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch7.tex
86 lines (65 loc) · 2.97 KB
/
ch7.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
% ch7.tex
% This work is licensed under the Creative Commons Attribution-Noncommercial-Share Alike 3.0 New Zealand License.
% To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/nz
% or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.
\chapter{A short chapter about Files}\label{ch:ashortchapteraboutfiles}\index{functions!file}
You probably know what a file is already.
\par
\noindent
If your parents have a home office, chances are they've got a file cabinet of some sort. Various important papers (mostly boring adult stuff) are stored in those cabinets, usually in cardboard folders labelled with letters of the alphabet, or months of the year. Files on a computer are rather similar to those cardboard folders. They have labels (the name of the file), and are used to store important information. The drawers on a file cabinet, which might be used to organise paperwork so they are easier to find, are similar to directories (or folders) on a computer.
\par
We've already created a file object, using Python, in the previous chapter. The example looked like this:
\begin{WINDOWS}
\begin{listing}
\begin{verbatim}
>>> f = open('c:\\test.txt')
>>> print(f.read())
\end{verbatim}
\end{listing}
\end{WINDOWS}
\begin{MAC}
\begin{listing}
\begin{verbatim}
>>> f = open('Desktop/test.txt')
>>> print(f.read())
\end{verbatim}
\end{listing}
\end{MAC}
\begin{LINUX}
\begin{listing}
\begin{verbatim}
>>> f = open('Desktop/test.txt')
>>> print(f.read())
\end{verbatim}
\end{listing}
\end{LINUX}
A file object doesn't just have the function \code{read}\index{functions!file!read}. After all, file cabinets wouldn't be very useful if you could only open a drawer and take papers out, but could never put them back in. We can create a new, empty file, by passing another parameter when we call the \code{file} function:
\begin{listing}
\begin{verbatim}
>>> f = open('myfile.txt', 'w')
\end{verbatim}
\end{listing}
'w' is the way we tell Python we want to write to the file object, and not read from it. We can now add information to the file using the function \code{write}\index{functions!file!write}.
\begin{listing}
\begin{verbatim}
>>> f = open('myfile.txt', 'w')
>>> f.write('this is a test file')
\end{verbatim}
\end{listing}
Then we need to tell Python when we're finished with the file, and don't want to write to it any more---we use the function \code{close}\index{functions!file!close} to do this.
\begin{listing}
\begin{verbatim}
>>> f = open('myfile.txt', 'w')
>>> f.write('this is a test file')
>>> f.close()
\end{verbatim}
\end{listing}
If you open the file using your favourite editor, you will see it contains the text: ``this is a test file''. Or better yet, we can use Python to read it in again:
\begin{listing}
\begin{verbatim}
>>> f = open('myfile.txt')
>>> print(f.read())
this is a test file
\end{verbatim}
\end{listing}
\newpage