Skip to content

Commit 79c4494

Browse files
committedJun 21, 2021
Created Open file
1 parent 6e09f28 commit 79c4494

File tree

7 files changed

+114
-0
lines changed

7 files changed

+114
-0
lines changed
 

‎File input output/Open file/input.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This is the first line of the file.
2+
Second line of the file.
3+
Here's another one!
4+
The last line.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is the entire file.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
type: edu
2+
files:
3+
- name: task.py
4+
visible: true
5+
placeholders:
6+
- offset: 152
7+
length: 37
8+
placeholder_text: '# Open the file input1.txt in read mode using the with statement'
9+
- offset: 396
10+
length: 15
11+
placeholder_text: '# Close the output file!'
12+
- name: tests.py
13+
visible: false
14+
- name: input.txt
15+
visible: true
16+
- name: input1.txt
17+
visible: true

‎File input output/Open file/task.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
## Open file
2+
Python has a number of built-in functions to read and write information to a file on your computer.
3+
4+
`open()` returns a file object, and is most commonly used with two arguments: `open(filename, mode)`:
5+
```python
6+
f = open('somefile.txt', 'w')
7+
```
8+
The first argument is a string containing the filename. The second argument is another string containing
9+
a few characters describing the way in which the file will be used. mode can be `"r"` when the file
10+
will only be read, `"w"` for only writing (an existing file with the same name will be erased), and
11+
'a' opens the file for appending; any data written to the file is automatically added to the end.
12+
`"r+"` opens the file for both reading and writing. The mode argument is optional; `"r"` will be assumed
13+
if it’s omitted.
14+
15+
It is good practice using the `with` keyword when dealing with file objects. The advantage is that the
16+
file is properly closed after its suite finishes.
17+
18+
```python
19+
with open('somefile.txt') as f:
20+
read_data = f.read()
21+
22+
# We can check that the file has been automatically closed.
23+
f.closed
24+
```
25+
```text
26+
True
27+
```
28+
**Important**: If you’re not using the `with` keyword, then you should call `f.close()` to close the file and
29+
immediately free up any system resources used by it. After a file object is closed, either by a `with` statement or by calling `f.close()`, attempts to use the file object
30+
will automatically fail.
31+
32+
In the code editor, open the file `input1.txt` in read mode properly using the `with` statement. Check out the
33+
name that is used for the file in the later code and use it. Afterwards, close the output file using the
34+
`close()` method.
35+
36+
<div class="hint">Supply the <code>r</code> argument to the method <code>open()</code>,
37+
just for the sake of practicing!</div>

‎File input output/Open file/task.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
with open('input.txt', 'r') as my_file:
2+
# Some action performed with the file, the read() method explained later.
3+
print(my_file.read(), '\n')
4+
5+
6+
with open('input1.txt', 'r') as file:
7+
print(file.read())
8+
9+
10+
outfile = open('outfile.txt', 'w') # opening the file in write mode (using `w` argument)
11+
outfile.write('Hello World') # writing to the file, the write() method is explained later.
12+
outfile.close()

‎File input output/Open file/tests.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from test_helper import run_common_tests, failed, passed, get_answer_placeholders, get_file_output
2+
3+
4+
def test_window():
5+
window = get_answer_placeholders()[0]
6+
if "with open(" in window:
7+
passed()
8+
else:
9+
failed("Use with open syntax.")
10+
11+
12+
def test_window1():
13+
window = get_answer_placeholders()[0]
14+
if "as file:" in window:
15+
passed()
16+
else:
17+
failed("The file should be named 'file'.")
18+
19+
20+
def test_window2():
21+
window = get_answer_placeholders()[0]
22+
if "'r'" in window or '"r"' in window:
23+
passed()
24+
else:
25+
failed("Add the 'r' argument!")
26+
27+
28+
def test_window3():
29+
window = get_answer_placeholders()[1]
30+
if 'outfile.close()' in window:
31+
passed()
32+
else:
33+
failed("Close the outfile properly, use the f.close() syntax.")
34+
35+
36+
if __name__ == '__main__':
37+
run_common_tests()
38+
test_window()
39+
test_window1()
40+
test_window2()
41+
test_window3()

‎File input output/lesson-info.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
custom_name: File input/output
22
content:
3+
- Open file
34
- Read file
45
- Write to file
6+

0 commit comments

Comments
 (0)
Please sign in to comment.