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 8595289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:23:58+00:00 2026-06-12T00:23:58+00:00

I need to patch os.listdir and other os functions to test my Python function.

  • 0

I need to patch os.listdir and other os functions to test my Python function. But when they’re patched, the import statement fails. Is it possible to patch this function only inside that single module, the real one, and leave tests.py work normally?

Here’s an example that breaks import:

import os
from mock import patch

# when both isdir and isfile are patched
# the function crashes
@patch('os.path.isdir', return_value=False)
@patch('os.path.isfile', return_value=False)
def test(*args):
    import ipdb; ipdb.set_trace()
    real_function(some_arguments)
    pass

test()

I want real_function to see a patched os.path, and tests to see normal functions.

here’s the traceback

  • 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-12T00:23:59+00:00Added an answer on June 12, 2026 at 12:23 am

    You can use patch as a context manager, so it will only apply to the code inside the with statement:

    import os
    from mock import patch
    
    def test(*args):
        import ipdb; ipdb.set_trace()
        with patch('os.path.isdir', return_value=False):
            with patch('os.path.isfile', return_value=False):
                real_function(some_arguments)
        pass
    
    test()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some function 'apply_patch': result_string = apply_patch('origin_file.txt', 'origin_file.txt.patch') or some commands (for windows
I'm not a java guy but I've inherited some code I need to patch
I need to apply a patch to the vim source code, but I am
I need to make a patch for someone (they are not using git) -
I need to get patch file for inital commit (which is not empty) for
I need to Monkey patch strftime in Ruby 1.8.7 with Rails 2.3 on Windows.
I need to write a small console app (patch) that turns off the print
Where do I include the Upgrade/Target Image in the following Patch XML? I need
The following code works, but I'm curious as to why I need the Path
I've got a Git repo from which I need to create a patch file

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.