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

  • Home
  • SEARCH
  • 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 6357759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:13:52+00:00 2026-05-24T23:13:52+00:00

I want to redirect the print to a .txt file using Python. I have

  • 0

I want to redirect the print to a .txt file using Python. I have a for loop, which will print the output for each of my .bam file while I want to redirect all output to one file. So I tried to put:

f = open('output.txt','w')
sys.stdout = f

at the beginning of my script. However I get nothing in the .txt file.
My script is:

#!/usr/bin/python

import os,sys
import subprocess
import glob
from os import path

f = open('output.txt','w')
sys.stdout = f

path= '/home/xxx/nearline/bamfiles'
bamfiles = glob.glob(path + '/*.bam')

for bamfile in bamfiles:
    filename = bamfile.split('/')[-1]
    print 'Filename:', filename
    samtoolsin = subprocess.Popen(["/share/bin/samtools/samtools","view",bamfile],
                                  stdout=subprocess.PIPE,bufsize=1)
    linelist= samtoolsin.stdout.readlines()
    print 'Readlines finished!'

So what’s the problem? Any other way besides this sys.stdout?

I need my result look like:

Filename: ERR001268.bam
Readlines finished!
Mean: 233
SD: 10
Interval is: (213, 252)
  • 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-24T23:13:55+00:00Added an answer on May 24, 2026 at 11:13 pm

    The most obvious way to do this would be to print to a file object:

    with open('out.txt', 'w') as f:
        print('Filename:', filename, file=f)  # Python 3.x
        # print >> f, 'Filename:', filename   # Python 2.x
    

    However, redirecting stdout also works for me. It is probably fine for a one-off script such as this:

    import sys
    
    orig_stdout = sys.stdout
    f = open('out.txt', 'w')
    sys.stdout = f
    
    for i in range(2):
        print('i = ', i)
    
    sys.stdout = orig_stdout
    f.close()
    

    Since Python 3.4 there’s a simple context manager available to do this in the standard library:

    from contextlib import redirect_stdout
    
    with open('out.txt', 'w') as f:
        with redirect_stdout(f):
            print('data')
    

    Redirecting externally from the shell itself is another option, and often preferable:

    ./script.py > out.txt
    

    Other questions:

    What is the first filename in your script? I don’t see it initialized.

    My first guess is that glob doesn’t find any bamfiles, and therefore the for loop doesn’t run. Check that the folder exists, and print out bamfiles in your script.

    Also, use os.path.join and os.path.basename to manipulate paths and filenames.

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

Sidebar

Related Questions

I have a flash file . In last frame i want to redirect it
I have a problem with my htaccess file in wordpress. I want to redirect
I have a method that where I want to redirect the user back to
I am using ASP.NET 2.0 C#. I want to redirect all request for my
I want: all links which not contained filename (not .html, .jpg, .png, .css) redirect
I want to loop over the contents of a text file and do a
I want to redirect a response to another URL while it contains some POST
I have a simple MVC 3 application. I want the site to automatically redirect
I want to redirect from an action in one controller to an action in
I want to add the link tags to redirect my web-site to my OpenID

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.