Python pathlib module

Quiz
•
Computers
•
Professional Development
•
Hard
Jeff Thuong
Used 4+ times
FREE Resource
11 questions
Show all answers
1.
MULTIPLE CHOICE QUESTION
30 sec • 5 pts
Which of the following is NOT a correct way to create a Path object
Path("my_file.txt")
Path("dir\\file.txt")
Path("dir2/hi.txt")
None (they are all correct)
Answer explanation
Path() supports both / and \
2.
MULTIPLE CHOICE QUESTION
45 sec • 5 pts
Which of the following way is NOT a correct way to combine paths?
Path("dir") / "subdir" / "hi.txt"
Path("dir") / Path("hi.txt")
Path("dir") / Path("C:/root.txt")
"dir1" / "dir2" / Path("hi.txt")
None (they are all correct)
Answer explanation
The operator / can combine a string or a Path (relative or absolute) to a given path.
Surprisingly, it also works with a string on the left "dir" / Path("...")
However, it does not work with two strings (i.e. "dir1" / "dir2" / Path("...")) because you cannot use / with two strings.
3.
MULTIPLE CHOICE QUESTION
30 sec • 5 pts
How to get the relative path from an absolute path abs_path, relative to curr_dir?
abs_path.relative(curr_dir)
curr_dir.relative(abs_path)
abs_path.relative_to(curr_dir)
curr_dir.relative_to(abs_path)
4.
MULTIPLE CHOICE QUESTION
45 sec • 5 pts
Which function could help you more easily get all PNG images in a directory image_dir but not in the subdirectories?
image_dir.iter_dir()
image_dir.glob()
image_dir.rglob()
image_dir.list_files()
Answer explanation
iter_dir: list all the files and directories but you cannot specify a pattern. You would have to filter by using `Path.suffix`
rglob: list with a pattern but recursively
glob: is the one you want
5.
MULTIPLE CHOICE QUESTION
30 sec • 5 pts
How to test if a file f is a Python file (based on the extension)?
f.extension == "py"
f.extension == ".py"
f.suffix == ".py"
f.suffix == "py"
Answer explanation
The extension is retrieved with .suffix and it contains the "." (so it should be compared to ".py")
6.
MULTIPLE CHOICE QUESTION
30 sec • 5 pts
How to get the path of a file that is in the same directory as a file f but with the name "hi.txt"?
f.with_name("hi.txt")
f.name("hi.txt")
I don't know
(Click me!🖱️)
7.
MULTIPLE CHOICE QUESTION
30 sec • 5 pts
How to create a directory d only if it does not exist, and with creation of parent directory if needed?
d.mkdir()
d.mkdir(exists=True, parents=True)
d.mkdir(exist_ok=True, parents=True)
mkdir(d, exist_ok=True, parent_ok=True)
Create a free account and access millions of resources
Similar Resources on Wayground
13 questions
HTML, CSS, JS основы

Quiz
•
Professional Development
15 questions
Linux Fundamentals Quiz 1

Quiz
•
Professional Development
11 questions
ITF - Quiz 12.2 - Windows File Sharing

Quiz
•
Professional Development
10 questions
Xpert H&P

Quiz
•
Professional Development
10 questions
Quiz sobre Gestión del catálogo

Quiz
•
Professional Development
7 questions
Python / Datnes, klasses

Quiz
•
Professional Development
10 questions
Evaluación de SO. Linux

Quiz
•
Professional Development
13 questions
Avaliação - Kodular

Quiz
•
Professional Development
Popular Resources on Wayground
10 questions
Lab Safety Procedures and Guidelines

Interactive video
•
6th - 10th Grade
10 questions
Nouns, nouns, nouns

Quiz
•
3rd Grade
10 questions
9/11 Experience and Reflections

Interactive video
•
10th - 12th Grade
25 questions
Multiplication Facts

Quiz
•
5th Grade
11 questions
All about me

Quiz
•
Professional Development
22 questions
Adding Integers

Quiz
•
6th Grade
15 questions
Subtracting Integers

Quiz
•
7th Grade
9 questions
Tips & Tricks

Lesson
•
6th - 8th Grade
Discover more resources for Computers
11 questions
All about me

Quiz
•
Professional Development
10 questions
How to Email your Teacher

Quiz
•
Professional Development
15 questions
Fun Random Trivia

Quiz
•
Professional Development
22 questions
Anne Bradstreet 1612-1672

Quiz
•
Professional Development
18 questions
Spanish Speaking Countries and Capitals

Quiz
•
KG - Professional Dev...
14 questions
Fall Trivia

Quiz
•
11th Grade - Professi...
15 questions
Disney Characters Quiz

Quiz
•
Professional Development
15 questions
Quiz to Highlight Q types & other great features in Wayground

Quiz
•
Professional Development