147. os.path for Path Manipulation
1. Joining Paths with os.path.join
os.path.joinimport os
def join_paths(*paths):
full_path = os.path.join(*paths)
print(f"Full Path: {full_path}")
# Example usage
join_paths("/home/user", "documents", "file.txt")Explanation:
2. Getting the Absolute Path with os.path.abspath
os.path.abspathimport os
def get_absolute_path(path):
abs_path = os.path.abspath(path)
print(f"Absolute Path: {abs_path}")
# Example usage
get_absolute_path("file.txt")Explanation:
3. Checking if a Path Exists with os.path.exists
os.path.existsExplanation:
4. Checking if a Path is a File with os.path.isfile
os.path.isfileExplanation:
5. Checking if a Path is a Directory with os.path.isdir
os.path.isdirExplanation:
6. Getting the Directory Name with os.path.dirname
os.path.dirnameExplanation:
7. Getting the File Name with os.path.basename
os.path.basenameExplanation:
8. Splitting Path into Directory and File with os.path.split
os.path.splitExplanation:
9. Checking if a Path is a Symbolic Link with os.path.islink
os.path.islinkExplanation:
10. Getting File Size with os.path.getsize
os.path.getsizeExplanation:
Conclusion:
Last updated