Skip to content

Commit 30c3f99

Browse files
committed
Merge branch 'revised-solutions-jan2020-update'
2 parents 3d1dbab + ffeca26 commit 30c3f99

File tree

61 files changed

+658
-568
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+658
-568
lines changed

ch09-lists-tuples-and-dictionaries/2-lists-are-mutable-sequences.py

+8
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,11 @@
3838
# Exercise 7
3939
# Verify that breakfast has three items using len()
4040
print(len(breakfast) == 3)
41+
42+
43+
# Exercise 8
44+
# Create a new list called `lengths` using a list
45+
# comprehension that contains the lengths of each
46+
# string in the `breakfast` list.
47+
lengths = [len(item) for item in breakfast]
48+
print(lengths)

ch11-file-input-and-output/1-read-and-write-simple-files.py

-45
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# 11.2 - Working With File Paths in Python
2+
# Solutions to review exercises
3+
4+
5+
# Exercise 1
6+
from pathlib import Path
7+
8+
file_path = Path.home() / "my_folder" / "my_file.txt"
9+
10+
11+
# Exercise 2
12+
print(file_path.exists())
13+
14+
15+
# Exercise 3
16+
print(file_path.name)
17+
18+
19+
# Exercise 4
20+
print(file_path.parent.name)

ch11-file-input-and-output/2-working-with-paths-in-python.py

-53
This file was deleted.

ch11-file-input-and-output/3-challenge-use-pattern-matching-to-delete-files.py

-21
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# 11.3 Common File System Operations
2+
# Solutions to Exercises
3+
4+
5+
# Exercise 1
6+
from pathlib import Path
7+
8+
new_dir = Path.home() / "my_folder"
9+
new_dir.mkdir()
10+
11+
12+
# Exercise 2
13+
file1 = new_dir / "file1.txt"
14+
file2 = new_dir / "file2.txt"
15+
image1 = new_dir / "image1.png"
16+
17+
file1.touch()
18+
file2.touch()
19+
image1.touch()
20+
21+
22+
# Exercise 3
23+
images_dir = new_dir / "images"
24+
images_dir.mkdir()
25+
image1.replace(images_dir / "image1.png")
26+
27+
28+
# Exercise 4
29+
file1.unlink()
30+
31+
32+
# # Exercise 5
33+
import shutil
34+
shutil.rmtree(new_dir)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# 11.4 Challenge: Move All Image Files To a New Directory
2+
# Solution to Challenge
3+
4+
from pathlib import Path
5+
6+
# Change this path to match the location on your computer
7+
documents_dir = (
8+
Path.home() /
9+
"python-basics-exercises" /
10+
"ch11-file-input-and-output" /
11+
"practice_files" /
12+
"documents"
13+
)
14+
15+
# Create an images/ directory in your home directory
16+
images_dir = Path.home() / "images"
17+
images_dir.mkdir(exist_ok=True)
18+
19+
# Search for image files in the documents directory and move
20+
# them to the images/ directory
21+
for path in documents_dir.rglob("*.*"):
22+
if path.suffix.lower() in [".png", ".jpg", ".gif"]:
23+
path.replace(images_dir / path.name)

ch11-file-input-and-output/4-read-and-write-csv-data.py

-33
This file was deleted.

ch11-file-input-and-output/5-challenge-create-a-high-scores-list-from-csv-data.py

-26
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# 11.5 - Reading and Writing Files
2+
# Solutions to Exercises
3+
4+
5+
# Exercise 1
6+
from pathlib import Path
7+
8+
starships = ["Discovery\n", "Enterprise\n", "Defiant\n", "Voyager"]
9+
10+
file_path = Path.home() / "starships.txt"
11+
with file_path.open(mode="w", encoding="utf-8") as file:
12+
file.writelines(starships)
13+
14+
15+
# Exercise 2
16+
with file_path.open(mode="r", encoding="utf-8") as file:
17+
for starship in file.readlines():
18+
print(starship, end="")
19+
20+
21+
# Exercise 3
22+
with file_path.open(mode="r", encoding="utf-8") as file:
23+
for starship in file.readlines():
24+
if starship.startswith("D"):
25+
print(starship, end="")

0 commit comments

Comments
 (0)