Improve way to deal with file paths on Windows, Mac and Linux;
Use environment variables.
👨💻 Solutions
Dealing with file paths on Windows, Mac and Linux
A easy way to deal with file paths is to use the python (3.4+) library pathlib. With pathlib we've just to use forward slashes (/). For example, the function Path() will convert forward slashes into the correct kind of slash for the current operating system:
from pathlib import Path
folder_path = Path("user/data")
print(folder_path)
Output:
#on Linux:
user/data
#on Windows:
user\data
If we need to add a sub path on to the path, we can use the / operator directly in the code:
from pathlib import Path
folder_path = Path("user/data")
print(folder_path / 'sub')
📋 Tasks Summary
👨💻 Solutions
Dealing with file paths on Windows, Mac and Linux
A easy way to deal with file paths is to use the python (3.4+) library pathlib. With pathlib we've just to use forward slashes (/). For example, the function Path() will convert forward slashes into the correct kind of slash for the current operating system:
Output:
If we need to add a sub path on to the path, we can use the / operator directly in the code:
Output: