Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit30c3f99

Browse files
committed
Merge branch 'revised-solutions-jan2020-update'
2 parents3d1dbab +ffeca26 commit30c3f99

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

Lines changed: 8 additions & 0 deletions
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)foriteminbreakfast]
48+
print(lengths)

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

Lines changed: 0 additions & 45 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
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+
frompathlibimportPath
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

Lines changed: 0 additions & 53 deletions
This file was deleted.

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

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
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+
frompathlibimportPath
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+
importshutil
34+
shutil.rmtree(new_dir)
Lines changed: 23 additions & 0 deletions
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+
frompathlibimportPath
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+
forpathindocuments_dir.rglob("*.*"):
22+
ifpath.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

Lines changed: 0 additions & 33 deletions
This file was deleted.

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

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
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+
frompathlibimportPath
7+
8+
starships= ["Discovery\n","Enterprise\n","Defiant\n","Voyager"]
9+
10+
file_path=Path.home()/"starships.txt"
11+
withfile_path.open(mode="w",encoding="utf-8")asfile:
12+
file.writelines(starships)
13+
14+
15+
# Exercise 2
16+
withfile_path.open(mode="r",encoding="utf-8")asfile:
17+
forstarshipinfile.readlines():
18+
print(starship,end="")
19+
20+
21+
# Exercise 3
22+
withfile_path.open(mode="r",encoding="utf-8")asfile:
23+
forstarshipinfile.readlines():
24+
ifstarship.startswith("D"):
25+
print(starship,end="")

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp