To create a path to a file or directory relative to the current working directory using the path
package in Python, you can follow these steps:
Path
class from the path
package:from path import Path
Path
object with the current working directory using the Path.cwd()
method:current_dir = Path.cwd()
/
operator to append the file or directory name to the path:file_path = current_dir / "file.txt"
directory_path = current_dir / "directory"
/
operator multiple times:subdirectory_path = current_dir / "directory" / "subdirectory"
str()
function:print(str(file_path))
print(str(directory_path))
print(str(subdirectory_path))
By following these steps, you will have created a path to a file or directory relative to the current working directory using the path
package in Python.