Skip to content

Commit 9454395

Browse files
committed
finished information for file input/output
1 parent f9926f7 commit 9454395

File tree

1 file changed

+54
-9
lines changed

1 file changed

+54
-9
lines changed

FileInputAndOutput/lesson_plan.txt

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ path. While if I am in the Trump folder, .\Documents\nuclear_launch_codes.txt
7171
is the relative path. We can try this out in the windows command line.
7272

7373
Show graphic/example to compare and contrast
74-
There are two shortcuts to represent this directory and the parent folder
74+
There are two shortcuts to represent this directory and the parent folder. We
75+
will be using these a lot.
76+
7577
. (dot) for this directory
7678

7779
.. (dot-dot) for parent folder
@@ -155,21 +157,64 @@ will **not** be working with *binary files* which are only computer readable
155157
files. If we tried to open a binary file to read, we will get garbled text.
156158

157159
We will be using three functions to read plain text files,
158-
**open()** - returns a *File* object
160+
161+
**open()** - returns a *File* object. Passing 'w' allows you to write into the
162+
file. Passing 'a' allows us to append into the object.
163+
We are going to be calling the next few methods on our file
164+
object.
165+
159166
**read()** or **write()** - reads from or writes to the file
167+
160168
**close()** - closes the *File* object which allows Python to save the file
161169

162-
### Opening Files with the open() Function
170+
Let's create our own files with python. Follow along with the Python
171+
interactive shell. Our code will work with any operating system.
172+
```python
173+
>> import os
174+
>> path = os.path.join('.', 'newFile.txt') # Why does this work everytime?
175+
>> myFile = open(path, 'w')
176+
>> myFile.write('Hello [insert name]\n')
177+
>> myFile.close()
178+
179+
>> helloFile = open(path, 'r')
180+
>> print(helloFile.read())
181+
Hello [insert name]
182+
>> helloFile.close()
183+
184+
>> editFile = open(path, 'a') # if we just passed 'w', we would overwrite our data
185+
>> editFile = write('I like [insert food]')
186+
>> editFile = close()
187+
188+
>> readFile = open(path)
189+
>> print(readFile.read())
190+
Hello [insertname]
191+
I like [insert food]
192+
>> readFile.close()
193+
```
194+
195+
# Activity
196+
197+
198+
# Summary
199+
Files and Folders are organized in a tree like structure. The path describes
200+
the location of the file or folder. The path can either be described with an
201+
absolute path or a relative path. The os.path module has a lot of functions we
202+
can use to manipulate file paths.
203+
204+
We can directly read and write plain text files. We first need to use the
205+
**open()** function to give us a **File** object. With the File object, we are
206+
able to **read()** from the file and **write()** to it. the **open()** function
207+
can be passed a 'read', 'write', or 'append' argument.
208+
209+
Knowing how to manipulate files is useful because we are now able to store data
210+
after the program is finished and when the computer restarts. From this
211+
foundation, we can learn how to manipulate whole files themselves; copying,
212+
deleting, renaming, moving them, and so forth in
213+
[Organizing Files](https://github.com/cppignite/python/wiki/Organizing-Files)
163214

164215

165-
os.path.abspath()
166-
os.path.basename()
167-
os.path.exists()
168216
file.startswith()
169217
file.endswith()
170-
open()
171-
write()
172-
os.path.join()
173218

174219

175220
Need

0 commit comments

Comments
 (0)