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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:48:26+00:00 2026-05-13T05:48:26+00:00

So I would like to grab stdout from a subprocess and then write the

  • 0

So I would like to grab stdout from a subprocess and then write the output to a file using python.

The problem I’m having is the stdout from the subprocess loses the formatting, it contains \n’s where there are newlines. I would like to write the output to a file with formatting intact, meaning instead of one line containg \n’s, the file contains newlines where there are \n’s.

Here is my existing code:

import os, subprocess
from cStringIO import StringIO

proc = subprocess.Popen('foo.exe', shell=True, stdout=subprocess.PIPE,)

stdout_value = proc.communicate()[0]

f=open('fooOut.txt', 'w')
f.write(str(repr(stdout_value)))
f.close()

Current File Text: abbbb\nabbbb\naaaaab

What I would like:

abbbb

bbbb

aaaaab

  • 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-13T05:48:26+00:00Added an answer on May 13, 2026 at 5:48 am

    Don’t call repr(), ie. just call

    f.write(stdout_value)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to grab data from a table without using regular expressions. I've
I would like to grab an audio file from the iOS device's iTunes library,
Would like to parse IPv4 address from exit-addresses . Format of the file: ExitNode
I m using codeigniter and would like to grab some user info with ajax.
I would like to grab data from a certain website periodically (every 2-3 hours).
I would like to grab all the image file names in an image directory
I would like to grab an element from a remote HTML page. As I
I would like to grab an existing stream from an IP-Camera delivering h264 encoded
I'm using ExtJS v2.0 . I would like to grab the gridfilter values and
I would like to grab a number from a string. The string is: responseID:

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.