Bird
Raised Fist0
Pythonprogramming~10 mins

Working with operating system paths in Python - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to join two parts of a file path using the os module.

Python
import os
path = os.path.[1]('folder', 'file.txt')
print(path)
Drag options to blanks, or click blank then click option'
Abasename
Bsplit
Cexists
Djoin
Attempts:
3 left
💡 Hint
Common Mistakes
Using os.path.split which breaks a path instead of joining.
Using os.path.exists which checks if a path exists, not joining.
2fill in blank
medium

Complete the code to get the directory name from a file path.

Python
import os
folder = os.path.[1]('/home/user/file.txt')
print(folder)
Drag options to blanks, or click blank then click option'
Adirname
Bbasename
Cjoin
Dsplitext
Attempts:
3 left
💡 Hint
Common Mistakes
Using basename which returns the file name, not the folder.
Using splitext which separates file extension.
3fill in blank
hard

Fix the error in the code to check if a path exists.

Python
import os
path = '/tmp/test.txt'
exists = os.path.[1](path)
print(exists)
Drag options to blanks, or click blank then click option'
Aexists
Bjoin
Cisfile
Ddirname
Attempts:
3 left
💡 Hint
Common Mistakes
Using join which combines paths, not checks existence.
Using dirname which gets folder name, not existence.
4fill in blank
hard

Fill both blanks to create a dictionary with file names as keys and their extensions as values.

Python
import os
files = ['doc.txt', 'image.png', 'script.pyc']
ext_dict = {f[1]: os.path.[2](f)[1] for f in files}
print(ext_dict)
Drag options to blanks, or click blank then click option'
A[:-4]
Bsplitext
Cbasename
D[-4:]
Attempts:
3 left
💡 Hint
Common Mistakes
Using basename which returns full file name including extension.
Using wrong slicing that does not remove extension correctly.
5fill in blank
hard

Fill all three blanks to filter files with '.txt' extension and get their absolute paths.

Python
import os
files = ['a.txt', 'b.py', 'c.txt']
txt_paths = [os.path.[1](f) for f in files if os.path.splitext(f)[[2]] == [3]]
print(txt_paths)
Drag options to blanks, or click blank then click option'
Aabspath
B1
C'.txt'
Dbasename
Attempts:
3 left
💡 Hint
Common Mistakes
Using basename which returns only file name, not full path.
Using index 0 which is file name without extension.
Comparing extension to wrong string.

Practice

(1/5)
1. Which function is used to safely combine folder and file names into a full path in Python?
easy
A. os.path.join()
B. os.path.exists()
C. os.path.basename()
D. os.path.dirname()

Solution

  1. Step 1: Understand purpose of os.path.join()

    It combines parts of a path into one full path, handling separators correctly.
  2. Step 2: Compare with other functions

    os.path.exists() checks if a path exists, basename() gets file name, dirname() gets folder name.
  3. Final Answer:

    os.path.join() -> Option A
  4. Quick Check:

    Combine paths = os.path.join() [OK]
Hint: Use os.path.join() to build paths safely [OK]
Common Mistakes:
  • Confusing join() with exists()
  • Using basename() to join paths
  • Using dirname() to combine paths
2. Which of the following is the correct syntax to get the folder name from a path stored in variable path?
easy
A. os.path.basename(path)
B. os.path.dirname(path)
C. os.path.join(path)
D. os.path.exists(path)

Solution

  1. Step 1: Identify function to get folder name

    os.path.dirname(path) returns the directory part of the path.
  2. Step 2: Check other options

    basename() returns file name, join() combines paths, exists() checks path existence.
  3. Final Answer:

    os.path.dirname(path) -> Option B
  4. Quick Check:

    Folder name = os.path.dirname(path) [OK]
Hint: Use os.path.dirname() to get folder name from path [OK]
Common Mistakes:
  • Using basename() to get folder
  • Calling join() with one argument
  • Confusing exists() with dirname()
3. What will be the output of this code?
import os
path = os.path.join('folder', 'subfolder', 'file.txt')
print(os.path.basename(path))
medium
A. folder
B. subfolder
C. file.txt
D. folder/subfolder/file.txt

Solution

  1. Step 1: Understand os.path.join()

    It creates 'folder/subfolder/file.txt' (with correct separator).
  2. Step 2: Understand os.path.basename()

    It returns the last part of the path, which is the file name 'file.txt'.
  3. Final Answer:

    file.txt -> Option C
  4. Quick Check:

    basename() returns file name [OK]
Hint: basename() returns last part (file) of path [OK]
Common Mistakes:
  • Expecting folder name instead of file
  • Confusing join() output with basename()
  • Printing full path instead of basename
4. What is wrong with this code snippet?
import os
path = os.path.join('folder', 'file.txt')
if os.path.exists:
    print('Path exists')
medium
A. os.path.exists is used without parentheses
B. os.path.join() cannot join two parts
C. print statement is missing parentheses
D. Variable path is not defined

Solution

  1. Step 1: Check usage of os.path.exists

    It is a function and must be called with parentheses and argument: os.path.exists(path).
  2. Step 2: Verify other parts

    join() usage is correct, print() has parentheses, path is defined.
  3. Final Answer:

    os.path.exists is used without parentheses -> Option A
  4. Quick Check:

    Call exists() with parentheses [OK]
Hint: Call os.path.exists() with path argument [OK]
Common Mistakes:
  • Forgetting () after exists
  • Passing no argument to exists()
  • Misusing join() function
5. You want to check if a file named data.csv exists inside a folder reports located in the user's home directory. Which code correctly builds the path and checks existence?
hard
A. os.path.exists(os.path.join(os.getcwd(), 'reports', 'data.csv'))
B. os.path.exists(os.path.join('reports', 'data.csv'))
C. os.path.exists('~/reports/data.csv')
D. os.path.exists(os.path.join(os.path.expanduser('~'), 'reports', 'data.csv'))

Solution

  1. Step 1: Get user's home directory

    Use os.path.expanduser('~') to get the home folder path.
  2. Step 2: Join home, reports, and file name

    Use os.path.join() to combine home path, 'reports', and 'data.csv'.
  3. Step 3: Check if the full path exists

    Pass the full joined path to os.path.exists() to check existence.
  4. Final Answer:

    os.path.exists(os.path.join(os.path.expanduser('~'), 'reports', 'data.csv')) -> Option D
  5. Quick Check:

    Expand user + join + exists = os.path.exists(os.path.join(os.path.expanduser('~'), 'reports', 'data.csv')) [OK]
Hint: Use expanduser('~') to get home folder before joining [OK]
Common Mistakes:
  • Using relative path without home folder
  • Using literal '~/...' without expanduser
  • Using current working directory instead of home