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 in Python by ana1504.k
• 7,870 points
116 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 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,810 points
135 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,420 points
93 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,420 points
1,347 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
96 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,140 points
267 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,560 points
31 views
+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,700 points
1,747 views
+4 votes
6 answers