function create_new_filename
Modifies a PDF filename by inserting '_fully_signed' before the file extension, leaving non-PDF filenames unchanged.
/tf/active/vicechatdev/mailsearch/copy_signed_documents.py
86 - 91
simple
Purpose
This function is designed to create a new filename for PDF documents that have been fully signed, following a naming convention that clearly identifies signed documents. It's typically used in document processing workflows where signed PDFs need to be distinguished from unsigned ones. The function safely handles both PDF and non-PDF filenames, returning non-PDF filenames unmodified.
Source Code
def create_new_filename(original_filename):
"""Add '_fully_signed' before the .pdf extension"""
if original_filename.endswith('.pdf'):
base = original_filename[:-4]
return f"{base}_fully_signed.pdf"
return original_filename
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
original_filename |
- | - | positional_or_keyword |
Parameter Details
original_filename: A string representing the original filename (with or without path). Expected to be a filename ending in '.pdf' for modification, though any string is accepted. The function checks for the '.pdf' extension (case-sensitive) and only modifies filenames that match this pattern.
Return Value
Returns a string representing the modified filename. If the input ends with '.pdf', returns the filename with '_fully_signed' inserted before the extension (e.g., 'document.pdf' becomes 'document_fully_signed.pdf'). If the input does not end with '.pdf', returns the original filename unchanged. The return type is always a string.
Usage Example
# Basic usage
original = 'contract.pdf'
new_name = create_new_filename(original)
print(new_name) # Output: 'contract_fully_signed.pdf'
# With path included
original_with_path = '/documents/agreement.pdf'
new_name_with_path = create_new_filename(original_with_path)
print(new_name_with_path) # Output: '/documents/agreement_fully_signed.pdf'
# Non-PDF file (unchanged)
text_file = 'readme.txt'
result = create_new_filename(text_file)
print(result) # Output: 'readme.txt'
# File with multiple dots
complex_name = 'report.v2.final.pdf'
result = create_new_filename(complex_name)
print(result) # Output: 'report.v2.final_fully_signed.pdf'
Best Practices
- The function performs case-sensitive matching for '.pdf' extension - files ending in '.PDF' or '.Pdf' will not be modified
- The function works with both simple filenames and full file paths, as it only modifies the last 4 characters if they match '.pdf'
- Consider validating that the original_filename is not None or empty before calling this function in production code
- This function only modifies the filename string; it does not perform any file system operations (no files are renamed or moved)
- If you need case-insensitive extension matching, consider using original_filename.lower().endswith('.pdf') in a modified version
- The function assumes the filename is a string; passing non-string types will raise an AttributeError
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function sanitize_filename 53.9% similar
-
function main_v44 48.9% similar
-
function add_watermark 46.7% similar
-
class PDFManipulator 44.7% similar
-
class HashGenerator 43.8% similar