- Get the filename, directory, extension from a path string in Python
- Difference in path separator by OS
- Get the filename (basename) from a path: os.path.basename()
- Filename with extension
- Filename without extension
- Get the directory (folder) name from a path: os.path.dirname()
- Get the file and directory name pair: os.path.split()
- Notes on when a path string indicates a directory
- Get the extension: os.path.splitext()
- Create a path string with a different extension
- Get the extension without dot (period)
- Examples of cases like .tar.gz
- Create a path string by combining the file and directory names: os.path.join()
- Create a path string for another file in the same directory
- Use different OS formats
- Examples for Windows
- Backslash and raw string
- Examples of getting file name, folder name, extension
- Get and join a drive letter: os.path.splitdrive()
- Get file path without file name
- 4 Answers 4
Get the filename, directory, extension from a path string in Python
In Python, you can get the filename (basename), directory (folder) name, and extension from a path string or join the strings to generate the path string with the os.path module in the standard library.
- Difference in path separator by OS
- Get the filename (basename) from a path: os.path.basename()
- Filename with extension
- Filename without extension
- Notes on when a path string indicates a directory
- Create a path string with a different extension
- Get the extension without dot (period)
- Examples of cases like .tar.gz
- Create a path string for another file in the same directory
- Backslash and raw string
- Examples of getting file name, folder name, extension
- Get and join a drive letter: os.path.splitdrive()
Use the following path string as an example.
import os filepath = './dir/subdir/filename.ext'
The sample code below is running on a Mac. Examples for Windows are shown at the end.
In Python 3.4 or later, you can also get the filename, directory (folder) name, extension, etc., with the pathlib module that treats paths as objects.
Difference in path separator by OS
The path separator varies depending on the OS. UNIX (including Mac) uses the slash / , while Windows uses the backslash \ .
You can get the separator in the OS running Python with os.sep or os.path.sep .
print(os.sep) # / print(os.sep is os.path.sep) # True
Get the filename (basename) from a path: os.path.basename()
Use os.path.basename() to get the filename (basename) from a path string.
Filename with extension
os.path.basename() returns the filename with the extension.
filepath = './dir/subdir/filename.ext' basename = os.path.basename(filepath) print(basename) # filename.ext print(type(basename)) #
Filename without extension
You can get the filename without the extension with os.path.splitext() described later.
filepath = './dir/subdir/filename.ext' basename_without_ext = os.path.splitext(os.path.basename(filepath))[0] print(basename_without_ext) # filename
os.path.splitext() split at the last (right) dot . . If you want to split by the first (left) dot . , use split() .
filepath_tar_gz = './dir/subdir/filename.tar.gz' print(os.path.splitext(os.path.basename(filepath_tar_gz))[0]) # filename.tar print(os.path.basename(filepath_tar_gz).split('.', 1)[0]) # filename
Get the directory (folder) name from a path: os.path.dirname()
Use os.path.dirname() to get the directory folder (name) from a path string.
filepath = './dir/subdir/filename.ext' dirname = os.path.dirname(filepath) print(dirname) # ./dir/subdir print(type(dirname)) #
If you want to get only the directory name directly above the file, use os.path.basename() .
subdirname = os.path.basename(os.path.dirname(filepath)) print(subdirname) # subdir
Get the file and directory name pair: os.path.split()
Use os.path.split() to get both the file and directory (folder) name.
os.path.split() returns a tuple of filename returned by os.path.basename() and directory name returned by os.path.dirname() .
filepath = './dir/subdir/filename.ext' base_dir_pair = os.path.split(filepath) print(base_dir_pair) # ('./dir/subdir', 'filename.ext') print(type(base_dir_pair)) # print(os.path.split(filepath)[0] == os.path.dirname(filepath)) # True print(os.path.split(filepath)[1] == os.path.basename(filepath)) # True
You can unpack tuple to assign to each variable.
dirname, basename = os.path.split(filepath) print(dirname) # ./dir/subdir print(basename) # filename.ext
Use os.path.join() described later to rejoin the file and directory names.
Notes on when a path string indicates a directory
Note that the result will differ for a path string indicating a directory (folder) based on the presence or absence of a separator at the end.
dirpath_without_sep = './dir/subdir' print(os.path.split(dirpath_without_sep)) # ('./dir', 'subdir') print(os.path.basename(dirpath_without_sep)) # subdir
If there is a separator at the end, use os.path.dirname() and os.path.basename() to get the bottom folder name.
dirpath_with_sep = './dir/subdir/' print(os.path.split(dirpath_with_sep)) # ('./dir/subdir', '') print(os.path.basename(os.path.dirname(dirpath_with_sep))) # subdir
Get the extension: os.path.splitext()
Use os.path.splitext() to get the extension.
os.path.splitext() splits the extension and others and returns it as a tuple. The extension contains the dot . .
filepath = './dir/subdir/filename.ext' root_ext_pair = os.path.splitext(filepath) print(root_ext_pair) # ('./dir/subdir/filename', '.ext') print(type(root_ext_pair)) #
You can concatenate with the + operator to return the original path string.
root, ext = os.path.splitext(filepath) print(root) # ./dir/subdir/filename print(ext) # .ext path = root + ext print(path) # ./dir/subdir/filename.ext
Create a path string with a different extension
To create a path string with only the extension changed from the original, concatenate the first element of the tuple returned by os.path.splitext() with any extension.
filepath = './dir/subdir/filename.ext' other_ext_filepath = os.path.splitext(filepath)[0] + '.jpg' print(other_ext_filepath) # ./dir/subdir/filename.jpg
Get the extension without dot (period)
If you want to get the extension without the dot (period) . , specify the second and subsequent strings with slice [1:] .
filepath = './dir/subdir/filename.ext' ext_without_dot = os.path.splitext(filepath)[1][1:] print(ext_without_dot) # ext
Examples of cases like .tar.gz
Note that os.path.splitext() splits at the last (right) dot . as demonstrated in the previous example. Be cautious with extensions like .tar.gz .
filepath_tar_gz = './dir/subdir/filename.tar.gz' print(os.path.splitext(filepath_tar_gz)) # ('./dir/subdir/filename.tar', '.gz')
If you want to split by the first (left) dot . in the file name, use the split() method of the string, but it doesn’t work if the directory name also contains the dot . .
print(filepath_tar_gz.split('.', 1)) # ['', '/dir/subdir/filename.tar.gz']
After splitting with os.path.split() , apply the split() method of the string and join with os.path.join() described later.
The string returned by split() does not contain a delimiter, so be careful if you want to get an extension with a dot . like os.path.splitext() .
dirname, basename = os.path.split(filepath_tar_gz) basename_without_ext, ext = basename.split('.', 1) path_without_ext = os.path.join(dirname, basename_without_ext) print(path_without_ext) # ./dir/subdir/filename print(ext) # tar.gz ext_with_dot = '.' + ext print(ext_with_dot) # .tar.gz
Create a path string by combining the file and directory names: os.path.join()
Use os.path.join() to join file and directory names to create a new path string.
path = os.path.join('dir', 'subdir', 'filename.ext') print(path) # dir/subdir/filename.ext
Create a path string for another file in the same directory
If you want to create a path string for another file in the same directory of one file, use os.path.dirname() and os.path.join() .
filepath = './dir/subdir/filename.ext' other_filepath = os.path.join(os.path.dirname(filepath), 'other_file.ext') print(other_filepath) # ./dir/subdir/other_file.ext
Use different OS formats
If you want to manipulate path strings in an OS format that is not the OS on which Python is currently running, import and use different modules instead of the os module.
- UNIX (including current Mac): posixpath
- Windows: ntpath
- Macintosh 9 and earlier: macpath
Since each module has the same interface as os.path , you can change the os.path part of the sample code so far to their module names (such as ntpath ).
Examples for Windows
The sample code below is running on Mac using the ntpath module mentioned above. When running on Windows, you can replace ntpath with os.path .
Backslash and raw string
The path separator in Windows is the backslash \ .
To write a backslash in a string, you need to write two backslashes to escape. print() outputs one backslash.
import ntpath print(ntpath.sep) # \ print('\\') # \ print(ntpath.sep is '\\') # True
Using a raw string ( r’xxx’ ) simplifies writing a Windows path, as it allows you to write a backslash directly. Raw strings and normal strings have equal value.
file_path = 'c:\\dir\\subdir\\filename.ext' file_path_raw = r'c:\dir\subdir\filename.ext' print(file_path == file_path_raw) # True
For more information about raw strings, see the following article.
Examples of getting file name, folder name, extension
These examples also work on Windows.
print(ntpath.basename(file_path)) # filename.ext print(ntpath.dirname(file_path)) # c:\dir\subdir print(ntpath.split(file_path)) # ('c:\\dir\\subdir', 'filename.ext')
Get and join a drive letter: os.path.splitdrive()
Use os.path.splitdrive() to get the drive letter. The sample code below uses ntpath.splitdrive() .
os.path.splitdrive() splits the drive letter including the colon : and others.
print(ntpath.splitdrive(file_path)) # ('c:', '\\dir\\subdir\\filename.ext')
If you want to get only the drive letter, select the first character.
drive_letter = ntpath.splitdrive(file_path)[0][0] print(drive_letter) # c
Be careful when joining drive characters.
If you pass it to os.path.join() as it is, it will not work.
print(ntpath.join('c:', 'dir', 'subdir', 'filename.ext')) # c:dir\subdir\filename.ext
You can also specify os.sep ( ntpath.sep in the sample code) in the argument of os.path.join() or add a separator to the drive letter.
print(ntpath.join('c:', ntpath.sep, 'dir', 'subdir', 'filename.ext')) # c:\dir\subdir\filename.ext print(ntpath.join('c:\\', 'dir', 'subdir', 'filename.ext')) # c:\dir\subdir\filename.ext
Get file path without file name
Don’t assume that the path separator is always / if you are planning to run this on different operating systems.
Its for an internal project where we could set a standard, but you are right, someone might input a filepath from a different OS where its \ instead of /
4 Answers 4
dirname, fname = os.path.split(fullpath)
Split the pathname path into a pair, (head, tail) where tail is the last pathname component and head is everything leading up to that. The tail part will never contain a slash; if path ends in a slash, tail will be empty. If there is no slash in path, head will be empty.
os.path is always the module suitable for the platform that the code is running on.
fullpath = '/path/to/some/file.jpg' import os os.path.dirname(fullpath)
Using pathlib you can get the path without the file name using the .parent attribute:
from pathlib import Path fullpath = Path("/path/to/some/file.jpg") filepath = str(fullpath.parent) # /path/to/some/
This handles both UNIX and Windows paths correctly.
fullpath = '/path/to/some/file.jpg' index = fullpath.rfind('/') fullpath[0:index]
Based on the users given and then criteria, this should be good. But I agree this would not work in case of different OS.
It doesn’t work correctly with my first example either. We should not suggest fail-prone methods when there is a documented, platform agnostic built-in method of doing something, such as the os.path.split method suggested by @LevLevitsky. The OP has suggested a similar method themselves but they are looking for a better method because they think it is open to errors.