Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8679385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:57:33+00:00 2026-06-12T20:57:33+00:00

I have two utility functions that either append or remove a pattern onto a

  • 0

I have two utility functions that either append or remove a pattern onto a set of items within a directory. The functionality is exactly the same apart from the line that does the renaming which makes me believe I can merge them into one function.

Here are the two functions:

def append_items(source, pattern, dirs = True, recurse = False):
    """
    Append the pattern to all items within a directory

    source = act on this directory
    pattern = add this to the start of the file
    dirs = apply to directorys
    recurse = work recursively 
    """
    for item in os.listdir(source):
        path =  os.path.join(source, item)
        if "svn" not in item:
            if os.path.isdir(path):
                # Recurse first
                if recurse:
                    append_items(path, pattern, dirs, recurse)
                if dirs:
                    rename(path, path + pattern)
            elif os.path.isfile(path):
                name, ext = os.path.splitext(item)
                # Append pattern, add extension back
                new_path = os.path.join(source, "%s%s" % (name, pattern) + ext)
                rename(path, new_path)

#----------------------------------------------------------------------------------------------------------

def remove_string_from_items(source, pattern, dirs = True, recurse = False):
    """
    Remove a pattern from all items within a directory

    source =  directory
    pattern = text to replace
    """
    for item in os.listdir(source):
        path =  os.path.join(source, item)
        if "svn" not in item:
            if os.path.isdir(path):
                # Recurse first
                if recurse:
                    remove_string_from_items(path, pattern, dirs, recurse)
                if dirs and pattern in item:
                    target = os.path.join(source, string.replace(item, pattern, ""))
                    rename(path, target)
            elif os.path.isfile(path) and pattern in item:
                target = os.path.join(source, string.replace(item, pattern, ""))
                rename(path, target)

Can anybody point me towards a cleaner solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T20:57:34+00:00Added an answer on June 12, 2026 at 8:57 pm

    The following should be equivalent to what you have, with less repeated code:

    def _append_or_remove(source, pattern, dirs = True, recurse = False, append = True):
        for item in os.listdir(source):
            path =  os.path.join(source, item)
            if "svn" not in item:
                if os.path.isdir(path):
                    # Recurse first
                    if recurse:
                        if append:
                            append_items(path, pattern, dirs, recurse)
                        else:
                            remove_string_from_items(path, pattern, dirs, recurse)
                    if dirs and pattern in item:
                        target = os.path.join(source, string.replace(item, pattern, ""))
                        rename(path, target)
                elif os.path.isfile(path) and pattern in item:
                    if append:
                        name, ext = os.path.splitext(item)
                        # Append pattern, add extension back
                        target = os.path.join(source, "%s%s" % (name, pattern) + ext)
                    else:
                        target = os.path.join(source, string.replace(item, pattern, ""))
                    rename(path, target)
    
    def append_items(source, pattern, dirs = True, recurse = False):
        """
        Append the pattern to all items within a directory
    
        source = act on this directory
        pattern = add this to the start of the file
        dirs = apply to directorys
        recurse = work recursively 
        """
        return _append_or_remove(source, pattern, dirs, recurse, True)
    
    def remove_string_from_items(source, pattern, dirs = True, recurse = False):
        """
        Remove a pattern from all items within a directory
    
        source =  directory
        pattern = text to replace
        """
        return _append_or_remove(source, pattern, dirs, recurse, False)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two different applications (site + some cron utility) that are using the
I have two queries that find both the Zip codes, and the States for
I have a vb.net 3.5 class library project that needs to reference two assemblies
I have two product to install one is server utility and other is client
I have two web site that are 99% similar. They share all of the
I have an application - more like a utility - that sits in a
I have a utility function that enumerates all values of a type that is
I have two tables named utilities and types that i am trying to access
I have a utility struct defined in a library API, that has four fields,
i have two files, utility.h and utility.cpp . i declare a struct BoundingSphere in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.