Python3 Os Path
# Python3 os.path() Module
## Python3.x Python3 os.path() Module
[ Python3 OS File/Directory Methods](#)
* * *
The `os.path` module is a part of Python's standard `os` module, specifically designed for manipulating and handling file paths.
`os.path` provides a powerful set of tools for performing various operations on file and directory paths, such as getting filenames, checking if a path exists, joining paths, normalizing paths, and more.
The `os.path` module performs well across different operating systems, allowing the same code to handle path-related issues when running on different platforms (like Windows, Linux, macOS).
Here are some commonly used methods of the os.path module:
### Path Operations
| **Method** | **Description** |
| --- | --- |
| `os.path.abspath(path)` | Converts a relative path to an absolute path. |
| `os.path.basename(path)` | Gets the last component of the path, i.e., the filename. |
| `os.path.dirname(path)` | Gets the directory component of the path. |
| `os.path.join(*paths)` | Joins multiple paths, automatically handling path separators. |
| `os.path.split(path)` | Splits the path into a tuple of directory and filename. |
| `os.path.splitext(path)` | Splits the path into a tuple of filename and extension. |
### Path Information Retrieval
| **Method** | **Description** |
| --- | --- |
| `os.path.exists(path)` | Checks if the path exists. |
| `os.path.isfile(path)` | Checks if the path is a file. |
| `os.path.isdir(path)` | Checks if the path is a directory. |
| `os.path.getsize(path)` | Gets the size of the file in bytes. |
| `os.path.getatime(path)` | Gets the last access time of the file. |
| `os.path.getmtime(path)` | Gets the last modification time of the file. |
| `os.path.getctime(path)` | Gets the creation time of the file (on some operating systems, this represents the last status change time). |
### Path Normalization
| **Method** | **Description** |
| --- | --- |
| `os.path.normpath(path)` | Normalizes the path, eliminating redundant separators and relative path markers. |
| `os.path.realpath(path)` | Gets the real path of the file, resolving symbolic links. |
| `os.path.relpath(path, start=os.curdir)` | Computes the relative path from `start` to `path`. |
### Path Comparison
| **Method** | **Description** |
| --- | --- |
| `os.path.commonpath(paths)` | Returns the common path from a sequence of paths. |
| `os.path.commonprefix(list)` | Returns the longest common prefix from a sequence of paths. |
| `os.path.samefile(path1, path2)` | Checks if two paths point to the same file. |
| `os.path.sameopenfile(fp1, fp2)` | Checks if two open file objects point to the same file. |
| `os.path.samestat(stat1, stat2)` | Checks if two files have the same `stat` result. |
### Platform-Dependent Functions
| **Method** | **Description** |
| --- | --- |
| `os.path.splitdrive(path)` | On Windows, returns a tuple of the drive/device part and the path part. |
| `os.path.splitunc(path)` | Splits the UNC path into the share device and path parts (Windows specific). |
### Examples
The following examples demonstrate the use of os.path-related methods:
## Example
```python
#!/usr/bin/env python
import os.path
# Current filename
print( __file__ )
# Absolute path of the current filename
print(os.path.abspath( __file__ ))
# Returns the path of the current file
print(os.path.dirname(os.path.abspath( __file__ )))
The following example outputs related information about a file.
`test.py` `/tutorial/tutorial-test-py/test.py` `/tutorial/tutorial-test-py`
## Example
```python
import os
print(os.path.basename('/root/tutorial.txt'))
print(os.path.dirname('/root/tutorial.txt'))
print(os.path.split('/root/tutorial.txt'))
print(os.path.join('root','test','tutorial.txt'))
Executing the above program outputs the following:
tutorial.txt
/root
('/root', 'tutorial.txt')
root/test/tutorial.txt
The following example outputs related information about a file.
## Example
```python
import os
import time
file='/root/tutorial.txt'
print(os.path.getatime(file))
print(os.path.getctime(file))
print(os.path.getmtime(file))
print(time.gmtime(os.path.getmtime(file)))
print(os.path.getsize(file))
print(os.path.abspath(file))
print(os.path.normpath(file))
Executing the above program outputs the following:
1539052805.5735736
1539052805.5775735
1539052805.5735736
time.struct_time(tm_year=2018, tm_mon=10, tm_mday=9, tm_hour=2, tm_min=40, tm_sec=5, tm_wday=1, tm_yday=282, tm_isdst=0)
7
/root/tutorial.txt
/root/tutorial.txt
[ Python3 OS File/Directory Methods](#)
YouTip