-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileIO_notes_starter.py
More file actions
49 lines (29 loc) · 1.42 KB
/
fileIO_notes_starter.py
File metadata and controls
49 lines (29 loc) · 1.42 KB
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
"""
Name:
Demonstrates reading, writing, and appending to a text file
"""
# 1 - Creating and writing to a file
print("Creating a text file object with the open() function \
and writing to it with the .write() method...")
# open() built-in function - 1st argument is file name to open, 2nd is the file access mode
# .write() method writes strings to a file
# 2 - Reading from a file
print("\nReading from the newly created file...")
# .read() method reads characters from a file and returns as a string
# .readline() method - Reading a line from a file
print("\nReading one line at a time...")
# Looping through a file object directly
print("\nLooping through the file, line by line...")
# .readlines() method - Reading a file into a list
print("\nReading the entire file into a list...")
# 3 - Write a file's contents using the .writelines() method
print("\nWriting a text file using the writelines() method...")
# 4 - Reading the new file using a 'with statement'
# Eliminates the need to keep track of opened files and remember to close them.
# File is automatically closed when the indented block ends.
print("\nReading and printing the new file using a 'with statement'...")
# 5 - Append access mode
# Appends ("a") new data to an existing .txt file
# without overwriting the old data, like in "w" mode.
# Append 3 more items to the grocery list and display the full list.
print ("Adding 3 more items...\n")