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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:00:16+00:00 2026-06-10T19:00:16+00:00

i am trying redirect output to task1.txt. normal print funcion working perfectly but text

  • 0

i am trying redirect output to task1.txt. normal print funcion working perfectly but text can’t be redicted with sys.stdout.

import random
import sys
num_lines = 10

# read the contents of your file into a list
sys.stdout = open('C:\\Dropbox\\Python\\task1.txt','w')
with open('master.txt', 'r') as f:
    lines = [L for L in f if L.strip()]  # store non-empty lines

# get the line numbers of lines that are not marked
candidates = [i for i, L in enumerate(lines) if not L.startswith("*")] 

# if there are too few candidates, simply select all
if len(candidates) > num_lines:
    selected = random.sample(candidates, num_lines) 
else:
    selected = candidates  # choose all

# print the lines that were selected
# write.selected(sys.stdout)
print ("".join(lines[i] for i in selected))

# Mark selected lines in original content
for i in selected:
    lines[i] = "*%s" % lines[i]  # prepend "*" to selected lines

# overwrite the file with modified content
with open('master.txt', 'w') as f:
    f.write("".join(lines))
  • 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-10T19:00:18+00:00Added an answer on June 10, 2026 at 7:00 pm

    Don’t re-assign sys.stdout. Instead, use the file option on the print() function:

    with open('C:\\Dropbox\\Python\\task1.txt','w') as output:
        print ("".join(lines[i] for i in selected), file=output)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to redirect the output from manage.py to a text file, but only
Trying to redirect a subprocess' output to a file. server.py: while 1: print Count
I'm trying to redirect some of the standard output to a text file, and
Iam trying to redirect Console.Out to two text files using Console.SetOut . Console.SetOut(File.CreateText(c:\\del1.txt)); Console.WriteLine(string1);
I am trying to redirect the output of an ftp script to a txt
I am trying to redirect output of stdout and stderr using a file. I
I've noticed strange behavior when trying to redirect the output form a node.js script
I am trying to redirect perl system command to output file with the following
I am trying to redirect the Console output of a C#/Mono application into a
I am trying to redirect the output from my DLL to an external console

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.