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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:28:52+00:00 2026-05-31T05:28:52+00:00

I am running a find command for a particular file that I know exists.

  • 0

I am running a find command for a particular file that I know exists. I would like to get the path to that file, because I don’t want to assume that I know where the file is located. My understanding is that I need to redirect stdout, run the command and capture the output, re-hook-up standard output, then retrieve the results. The problem comes when I retrieve the results… I can’t decipher them:

import os
from cStringIO import StringIO
stdout_backup = sys.stdout #Backup standard output
stdout_output = StringIO() 
sys.stdout = stdout_output #Redirect standard output
os.system("find . -name 'foobar.ext' -print") #Find a known file
sys.stdout = stdout_backup #re-hook-up standard output as top priority
paths_to_file = stdout_ouput.get_value() #Retrieve results

I find all the paths I could want, the trouble is that paths_to_file yields this:

Out[9]: '\n\x01\x1b[0;32m\x02In [\x01\x1b[1;32m\x027\x01\x1b[0;32m\x02]: \x01\x1b[0m\x02\n\x01\x1b[0;32m\x02In [\x01\x1b[1;32m\x028\x01\x1b[0;32m\x02]: \x01\x1b[0m\x02'

I have no idea what to do with this. What I wanted was something like what the print command provides:

./Work/Halpin Programs/Servers/selenium-server.jar

How do I make that output usable for opening a file? If I can get what the print command yeilds, I can open the file I want.

Please reorient the question if I am misguided. Thank you!

  • 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-05-31T05:28:52+00:00Added an answer on May 31, 2026 at 5:28 am

    You cannot capture the output of a subprocess by changing sys.stdout. What you captured seems to be some ANSI escape sequences from your interactive Python interpreter (IPython?).

    To get the output of an external command, you should use subprocess.check_output():

    paths = subprocess.check_output(["find", ".", "-name", "foobar.ext"])
    

    In this particular case, I usually wouldn’t call an external command at all, but rather use os.walk() to find the files from right within the Python process.

    Edit: Here’s how to use os.walk() to find the files:

    def find(path, pattern):
        for root, dirs, files in os.walk(path):
            for match in fnmatch.filter(files, pattern):
                yield os.path.join(root, match)
    
    paths = list(find(".", "foobar.ext"))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I find the full path of the currently running Python interpreter from
After running the command find . -name R*VER -mtime +1 -exec sh -c 'grep
I'm trying to find a way to invoke an interactive command-line program so that
I want to find the exact time where a media file is currently paused
I am running a robocopy job through a batch file that is scheduled to
I have two search/replace commands that I find myself running in vim fairly often
I'm running the find command to find certain files, but some files in sub-directories
I'm running a script from crontab that will just ssh and run a command
I find myself running scripts and copy-pasting the output of these runs into emails
I am using running a simple find all and paginating with willpaginate, but I'd

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.