pathlib

When working with file paths in Python, the pathlib module provides a great way of dealing with file paths in an object-oriented way. This is an improvement over using string manipulation for file paths, which can be error-prone and less readable:
from pathlib import Path

p = Path('.')      # '.' represents the current directory
print(p, type(p))  # <class 'pathlib.PosixPath'> or <class 'pathlib.WindowsPath'> depending on your operating system
You can join paths using the / operator. For instance, to represent a file example.txt in the current directory, you can do:
file_path = p / 'example.txt'  # constructs a new path
print(file_path)               # example.txt
The / operator works regardless of the underlying operating system and uses the correct path separator. Notice how the / operator is actually the __truediv__() magic method in the Path class, which makes working with Path objects very intuitive.
To get an absolute path from a Path object, you can use the absolute() method:
abs_path = file_path.absolute()  # get the absolute path
print(abs_path)                  # prints the absolute path, like /home/user/example.txt on Unix or C:\\user\\example.txt on Windows
The Path class has many useful methods for common operations:
  • Path.cwd(): returns the current working directory as a new Path object
  • Path.home(): returns the home directory as a new Path object
  • Path.exists(): checks if the path exists
  • Path.is_file(): checks if the path is a file
  • Path.is_dir(): checks if the path is a directory
  • Path.mkdir(): makes a new directory at the path
  • Path.glob(): returns a generator yielding all matching files in the directory
Let's see how some of these methods are used:
print(Path.cwd())             # prints current working directory
print(Path.home())            # prints home directory

if not file_path.exists():    # check if a file exists
    print(f'The file {file_path} does not exist.')

if file_path.is_file():       # check if it is a file
    print(f'{file_path} is a file.')

new_dir = p / 'new_dir'
new_dir.mkdir(exist_ok=True)  # create a new directory, do nothing if it exists

for file in p.glob('*.txt'):  # iterate over all .txt files in the current directory
    print(file)
Remember to replace p with your Path object and 'example.txt' with your file name as appropriate. The pathlib module is a powerful tool for file and directory manipulation in Python.

Challenge: Relative to Absolute

The advent of digital archives has made the management of a huge number of documents much more accessible. However, managing paths to these documents can sometimes be a bit tricky, especially when dealing with relative and absolute paths. You work as a software developer at a digital archive company. Your task is to build a program that can convert relative paths to absolute paths to ensure proper document tracking and accessibility.
Given a relative path, your program should print its corresponding absolute path.
The only line of the input contains a single relative path.
The output of your program should be the absolute path corresponding to the inputted relative path.
Input
Output
./documents/report.pdf
/tmp/documents/report.pdf
folder2/file2.txt
/tmp/folder2/file2.txt
 

Constraints

Time limit: 2 seconds

Memory limit: 512 MB

Output limit: 1 MB

To check your solution you need to sign in
Sign in to continue