Courseiva
StringseasyMultiple ChoiceObjective-mapped

PCAP Strings Practice Question

A function receives a file path like '/home/user/docs/file.txt' and needs to return the path without the file extension, e.g., '/home/user/docs/file'. Which code reliably removes only the last dot extension, even if the directory names contain dots?

⚠ Common exam trap

The Python PCAP exam often tests the distinction between `split` and `rsplit` with the maxsplit parameter, and the trap here is that candidates mistakenly use `split('.')[0]` or `path[:path.find('.')]`, which fail when directory names contain dots because they target the first dot instead of the last.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

path.rsplit('.', 1)[0]

`rsplit('.', 1)` splits the string from the right, limiting the split to exactly one occurrence, which isolates the file extension (the part after the last dot) and returns everything before it. This reliably removes only the last dot extension, even if directory names contain dots, because it targets the final dot in the path.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • path.split('.')[0]

    Why it's wrong here

    path.split('.') breaks the entire path into segments by every dot and returns a list; indexing [0] then takes the text before the very first dot anywhere in the string. This is unreliable for stripping an extension because a dot may appear in a parent directory, as in '/home/user.name/file.txt', where the result becomes '/home/user', discarding the rest of the path along with the actual filename. Even if no directory dot exists, this method only works by assuming the first dot is the extension delimiter, which is not guaranteed in filesystem paths.

  • path.rsplit('.', 1)[0]

    Why this is correct

    path.rsplit('.', 1) splits from the right side using a maxsplit of 1, so it stops after encountering the last dot in the string, and [0] gives the substring before that final dot. This correctly removes only the extension from a path such as '/home/user/file.txt', producing '/home/user/file' while preserving any dots in the directory path, as with '/home/user.name/file.txt' -> '/home/user.name/file'. It is the string-method idiom for stripping a trailing extension, although os.path.splitext is the more robust alternative in practice.

  • path.replace('.', '', 1)

    Why it's wrong here

    path.replace('.', '', 1) replaces the first occurrence of the dot character with an empty string, effectively deleting that single dot rather than stripping an extension. For '/home/user/file.txt' it yields '/home/user/filetxt', leaving the extension letters 'txt' attached to the file name, so the suffix is not removed at all. Additionally, if the first dot appears in a directory name, this operation mutates the path there instead of touching the extension, making the result even further from the intended file base.

  • path[:path.find('.')]

    Why it's wrong here

    path[:path.find('.')] uses str.find to locate the smallest index at which a dot appears in the entire path, then slices from the beginning up to but not including that index. Because find searches left-to-right, it targets the first dot rather than the last, so any earlier dot in a directory path, such as '/home/user.name/docs/file.txt', causes the slice to stop prematurely and return '/home/user' instead of the file's base name. This is conceptually the opposite of rsplit('.', 1), which deliberately looks at the rightmost dot.

About these practice questions

One of 169 original PCAP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This PCAP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCAP exam.