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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:52:52+00:00 2026-05-25T12:52:52+00:00

I can successfully redirect my output to a file, however this appears to overwrite

  • 0

I can successfully redirect my output to a file, however this appears to overwrite the file’s existing data:

import subprocess
outfile = open('test','w') #same with "w" or "a" as opening mode
outfile.write('Hello')
subprocess.Popen('ls',stdout=outfile)

will remove the 'Hello' line from the file.

I guess a workaround is to store the output elsewhere as a string or something (it won’t be too long), and append this manually with outfile.write(thestring) – but I was wondering if I am missing something within the module that facilitates this.

  • 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-25T12:52:52+00:00Added an answer on May 25, 2026 at 12:52 pm

    You sure can append the output of subprocess.Popen to a file, and I make a daily use of it. Here’s how I do it:

    log = open('some file.txt', 'a')  # so that data written to it will be appended
    c = subprocess.Popen(['dir', '/p'], stdout=log, stderr=log, shell=True)
    

    (of course, this is a dummy example, I’m not using subprocess to list files…)

    By the way, other objects behaving like file (with write() method in particular) could replace this log item, so you can buffer the output, and do whatever you want with it (write to file, display, etc) [but this seems not so easy, see my comment below].

    Note: what may be misleading, is the fact that subprocess, for some reason I don’t understand, will write before what you want to write. So, here’s the way to use this:

    log = open('some file.txt', 'a')
    log.write('some text, as header of the file\n')
    log.flush()  # <-- here's something not to forget!
    c = subprocess.Popen(['dir', '/p'], stdout=log, stderr=log, shell=True)
    

    So the hint is: do not forget to flush the output!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an HBITMAP containing alpha channel data. I can successfully render this using
You can see I can successfully redirect values from one to this page. $shipping_first_name
Having integrated merb_auth_password_slice as per the README, I can successfully login as redirect_after_login is
I can successfully connect to MySQL from a DOS prompt, but when I try
I can successfully include main jquery files in /Scripts, but if my custom test.js
Can anyone explain how I can successfully get my processes communicating? I find the
the title already tells the question: you can successfully call the Print-Method on a
i have an iphone 3g and can successfully send text messages using the PHP
Symptom: Some users cannot successfully POST, most can successfully POST. When the error occurs,
Title says what i'm trying to do. I can successfully generate an assembly if

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.