freopen in python

0 votes
Is there anything in python that can replicate the functionality of freopen() in C or C++? what should i do if I want to replicate the functionality of:

freopen("input.txt","r",stdin);
 

and

freopen("output.txt","w",stdout);
 

And also use the same (standard) functions for console I/O for file I/O. how can i do it?
Jan 3, 2019 in Python by ana1504.k
• 7,910 points
1,452 views

1 answer to this question.

0 votes
sys.stdout is simply file object, so, you can reopen it to another destination

out = sys.stdout
sys.stdout = open('output.txt', 'w')
// do some work
sys.stdout = out
 

out is only for recovering sys.stdout destination to default after work
answered Jan 3, 2019 by anonymous

Related Questions In Python

+3 votes
7 answers

How can I rename a file in Python?

yes, you can use "os.rename" for that. ...READ MORE

answered Mar 31, 2018 in Python by DareDev
• 6,890 points
19,891 views
+2 votes
2 answers

How can I create a new file in Python?

You can try the below code which ...READ MORE

answered Mar 31, 2018 in Python by anto.trigg4
• 3,440 points
1,218 views
+2 votes
3 answers

what is the practical use of polymorphism in Python?

Polymorphism is the ability to present the ...READ MORE

answered Mar 31, 2018 in Python by anto.trigg4
• 3,440 points
4,757 views
+2 votes
2 answers

Error while printing hello world in python.

You must be trying this command in ...READ MORE

answered Mar 31, 2018 in Python by GandalfDwhite
• 1,320 points
6,235 views
0 votes
1 answer

How to write to CSV line by line in python

The simple way of doing this will ...READ MORE

answered Nov 15, 2018 in Python by findingbugs
• 3,260 points
5,582 views
0 votes
1 answer

Open file in Python

There is this code I used for ...READ MORE

answered Nov 16, 2018 in Python by Jino
• 5,820 points
744 views
0 votes
2 answers
+3 votes
5 answers

How to read multiple data files in python

Firstly we will import pandas to read ...READ MORE

answered Apr 6, 2018 in Python by DeepCoder786
• 1,720 points
15,126 views
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
3,935 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP