> print (Path ( 'usr' ) / 'bin' / 'spam' ) # usr/bin/spam Pathlib also provides a shortcut to joinpath using the / operator: > from pathlib import Path join ( 'usr', 'bin', 'spam' ) # 'usr\\bin\\spam'Īnd using pathlib on *nix: > from pathlib import Path Using os.path.join on Windows: > import os We will showcase how to deal with both, os.path.join and ![]() Joining paths can be a headache if your code needs to work on different platforms.įortunately, Python provides easy ways to handle this. On Unix based operating system such as macOS, Linux, and BSDs, the forward slash ( /) is used as the path separator. On Windows, paths are written using backslashes ( \) as the separator between folder names. dst/target_2.txt # read target file: # !! This is "target_2.The `pathlib` module was added in Python 3.4, offering an object-oriented way to handle file system paths. dst/target_2.txt # read target file: # !! This is "target_2.txt" !! # getcwd: /Users/mbp/Documents/my-project/python-snippets/notebook/data/src # target_path_1: target_1.txt # read target file: # !! This is "target_1.txt" !! # target_path_2. # getcwd: /Users/mbp/Documents/my-project/python-snippets/notebook/data/src # _file_: file_path.py # basename: file_path.py # dirname: # abspath: /Users/mbp/Documents/my-project/python-snippets/notebook/data/src/file_path.py # abs dirname: /Users/mbp/Documents/my-project/python-snippets/notebook/data/src # target_path_1: target_1.txt # read target file: # !! This is "target_1.txt" !! # target_path_2. Pwd # /Users/mbp/Documents/my-project/python-snippets/notebook/data/src dst/target_2.txt # read target file: # !! This is "target_2.txt" !! ![]() ![]() # getcwd: /Users/mbp/Documents/my-project/python-snippets/notebook # _file_: /Users/mbp/Documents/my-project/python-snippets/notebook/data/src/file_path.py # basename: file_path.py # dirname: /Users/mbp/Documents/my-project/python-snippets/notebook/data/src # abspath: /Users/mbp/Documents/my-project/python-snippets/notebook/data/src/file_path.py # abs dirname: /Users/mbp/Documents/my-project/python-snippets/notebook/data/src # target_path_1: /Users/mbp/Documents/my-project/python-snippets/notebook/data/src/target_1.txt # read target file: # !! This is "target_1.txt" !! # target_path_2: /Users/mbp/Documents/my-project/python-snippets/notebook/data/src/./dst/target_2.txt # normalize : /Users/mbp/Documents/my-project/python-snippets/notebook/data/dst/target_2.txt # read target file: # !! This is "target_2.txt" !! # getcwd: /Users/mbp/Documents/my-project/python-snippets/notebook/data/src # target_path_1: target_1.txt # read target file: # !! This is "target_1.txt" !! # target_path_2. Python3 /Users/mbp/Documents/my-project/python-snippets/notebook/data/src/file_path.py # getcwd: /Users/mbp/Documents/my-project/python-snippets/notebook # _file_: data/src/file_path.py # basename: file_path.py # dirname: data/src # abspath: /Users/mbp/Documents/my-project/python-snippets/notebook/data/src/file_path.py # abs dirname: /Users/mbp/Documents/my-project/python-snippets/notebook/data/src # target_path_1: data/src/target_1.txt # read target file: # !! This is "target_1.txt" !! # target_path_2: data/src/./dst/target_2.txt # normalize : data/dst/target_2.txt # read target file: # !! This is "target_2.txt" !! # getcwd: /Users/mbp/Documents/my-project/python-snippets/notebook/data/src # target_path_1: target_1.txt # read target file: # !! This is "target_1.txt" !! # target_path_2. Pwd # /Users/mbp/Documents/my-project/python-snippets/notebook
0 Comments
Leave a Reply. |