GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Python os.extsep Function - Complete Guide Python os.extsep FunctionLast modified April 11, 2025
This comprehensive guide explores Python's Basic Definitions
The This constant is part of Python's os module and is used in path manipulation functions. It helps create platform-independent code for handling file extensions. Basic Usage of os.extsep
The simplest use of basic_usage.py
import os
filename = "document"
extension = "txt"
# Construct full filename with extension
fullname = filename + os.extsep + extension
print(f"Full filename: {fullname}")
# Example with multiple extensions
archive_name = "backup"
extensions = ["tar", "gz"]
full_archive = archive_name + os.extsep + os.extsep.join(extensions)
print(f"Archive name: {full_archive}")
This example demonstrates constructing filenames with extensions using
Using Splitting Filename and Extension
split_extension.py
import os
def split_extension(path):
if os.extsep in path:
return path.rsplit(os.extsep, 1)
return path, ""
filename = "report.pdf"
name, ext = split_extension(filename)
print(f"Filename: {name}, Extension: {ext}")
# Compare with os.path.splitext
name2, ext2 = os.path.splitext(filename)
print(f"os.path.splitext: {name2}, {ext2}")
This shows a custom implementation of extension splitting using
The built-in function is generally preferred as it handles edge cases better,
but understanding the manual approach helps appreciate Validating File Extensions
validate_extension.py
import os
def has_valid_extension(filename, valid_extensions):
if os.extsep not in filename:
return False
_, ext = filename.rsplit(os.extsep, 1)
return ext.lower() in valid_extensions
allowed = ["jpg", "png", "gif"]
filenames = ["image.jpg", "picture.png", "document.pdf", "data"]
for name in filenames:
valid = has_valid_extension(name, allowed)
print(f"{name}: {'Valid' if valid else 'Invalid'}")
This function checks if a filename's extension is in the allowed list. It uses
The example handles cases where there is no extension and performs case- insensitive comparison by converting to lowercase. Changing File Extensions
change_extension.py
import os
def replace_extension(filename, new_ext):
if os.extsep in filename:
base, _ = filename.rsplit(os.extsep, 1)
return base + os.extsep + new_ext
return filename + os.extsep + new_ext
files = ["data.txt", "config.yaml", "readme"]
new_ext = "bak"
for file in files:
new_name = replace_extension(file, new_ext)
print(f"{file} -> {new_name}")
This function replaces the extension of a filename with a new one. If the original has no extension, it simply adds the new one. The code preserves the base name while ensuring proper use of the extension separator character defined by the operating system. Handling Hidden Files on Unix
On Unix systems, files starting with a dot are hidden. This example shows how
hidden_files.py
import os
def is_hidden_file(path):
filename = os.path.basename(path)
return filename.startswith(os.extsep)
def get_real_extension(path):
if is_hidden_file(path):
parts = path.split(os.extsep)
if len(parts) > 2: # Has actual extension
return parts[-1]
return ""
else:
_, ext = os.path.splitext(path)
return ext[1:] if ext else ""
test_files = [".bashrc", ".profile.config", "normal.txt", "noext"]
for file in test_files:
print(f"{file}: Hidden={is_hidden_file(file)}, Ext={get_real_extension(file)}")
This demonstrates handling Unix hidden files (dotfiles) while still properly identifying actual file extensions when they exist. The code distinguishes between the leading dot of hidden files and extension separators that appear later in the filename. Platform-Specific Behavior
While platform_specific.py
import os
import platform
print(f"Current platform: {platform.system()}")
print(f"os.extsep value: '{os.extsep}'")
def platform_safe_extension(base, ext):
return f"{base}{os.extsep}{ext}"
# Example usage
filename = platform_safe_extension("document", "txt")
print(f"Created filename: {filename}")
# Demonstrate with hypothetical different separator
original_extsep = os.extsep
try:
os.extsep = '#' # Simulate different platform
alt_filename = platform_safe_extension("test", "data")
print(f"With alternate separator: {alt_filename}")
finally:
os.extsep = original_extsep # Restore
This shows how The example includes a simulation of how the code would behave if the separator were different, though this is just for demonstration purposes. Working with Multiple Extensions
Some files have multiple extensions (e.g., .tar.gz). This example shows how to
handle such cases using multiple_extensions.py
import os
def split_all_extensions(path):
parts = []
while True:
path, ext = os.path.splitext(path)
if not ext:
break
parts.append(ext[len(os.extsep):]) # Remove separator
return path, parts[::-1] # Reverse to maintain order
files = ["archive.tar.gz", "backup.zip", "multi.part1.rar", "noext"]
for file in files:
base, exts = split_all_extensions(file)
print(f"{file} -> Base: {base}, Extensions: {exts}")
This function splits a filename into its base name and all extensions, handling multiple extension segments correctly. The result shows extensions in the order they appear in the filename (from right to left), which is typically how they're interpreted by systems. Security Considerations
Best Practices
Source ReferencesAuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. List all Python tutorials. |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |