Python tutorial 21: Python Print To File by Manish Sharma
Key Takeaways
Demonstrates how to redirect Python print output to a file using the print function
Full Transcript
let's start from where we left off last week today I will show you how to permanently redirect the output stream to file in Python programming I will try to keep it short and easy to understand like always let's do it what's up Internet I am manish from rebellionrider.com where we make learning computer programming form as mentioned earlier in this tutorial I will show you a hassle-free process of how to save the output of all the Python print statements into a file but first please consider subscribing to add notified whenever I upload information pack the Python programming tutorials that make learning form now let's start right in front of you is the code which we wrote last week the problem with it is that the output of only those print statements is getting saved permanently into a file where file parameter has been used for example in this code the output of the first print statement will get saved into the file whereas the output of the second print statement will get displayed back to the user on the computer screen this is all because of the file parameter which we have used in the first print statement but not in the second one now suppose I have a Python program which has ten print statements I want to save the output returned by all these print statements into a file on my operating system now the question here is do I need to use the file parameter with all of them or is there an alternative way of doing that well there is nothing wrong in doing that but in case if you don't want to then you can go the old-school way let's learn what is that first let's create a new worksheet for today's tutorial for that let's go to our project directory Python tutorials which we created last week here we are let's name this file duty 21 dot py now let's open it I will be using atom editor you can use whatever editor or IDE you want here we go now let's write some code here we have the first part of the program in the first line I am importing this module this is an inbuilt module of python libraries in the second statement I am redirecting the default output stream to a file this is a very important statement so let me explain it to you as you can see it's an assignment statement on the left hand side of the assignment operator we have sis not STD out STD out here is a file object if not set to anything then by default it sends the output to the default output stream which is almost always the screen of your system but we can reset it and that is what we are doing in this statement now let us come to the right-hand side of the assignment operator on the right hand side we have the Python open function we already know that this function returns a file object of the file whose name we have specified into it as a result on execution of this statement the interpreter will reset the default behavior of the STD out from printing the output on the screen to saving it into a file which you have is specified into the Python open function you will understand it when I will complete this code so let's write a print statement now as you can see in this Python print function call I have not specified the file parameter which means the output returned by this function should be displayed to us on the screen this is its usual behavior let's execute and see what happens but before that let me tell you this code has a problem to find out what is that keep watching now let's execute the code execution done but where is the output output is not on the screen because in the second statement we change the default output stream from printing the output on the screen to saving it into the file which means our output should be saved into the file tu t20 underscore output dot txt let's check for that I need to go into my current project directory which is Python tutorials where I created this Python file in the starting of the video here i am here i have the tu t20 underscore output dot txt file let's open it and check if it has our output saved into it or not here is our output now the question here is what if we have more than one print statements will the output of all those statements also get saved into the file let's write a few more print statements to find out the answer now we have total four Python print function calls here I have not used a file parameter in any of these calls now let's execute and see if their outputs also get saved into the file execution done let's check the file tud twenty underscore output dot txt here is our output can you guess why the string from the first Python print statement got printed twice yeah comment yes if you know the answer otherwise check out the comment section I will leave the explanation there so that's how we redirect the output stream from displaying the result on the screen to saving it into a file not to mention this redirection is permanent now let's talk about the problem with this code which I mentioned earlier before I come to that I just want to remind you to check out the corresponding block of this tutorial for detail knowledge that being said let's come to the topic now suppose you added one more print statement to your program and you don't want to save that into the file rather you want to display it back to the user let's add that Python print function call first this print statement is no different than the rest of the others hence on execution the output of this Python print statement will also get saved into the file which we don't want so what options do we have for that I know most of you will suggest saving sis not STD out into sis not STD out like this looks legit let's execute and see if it works or not execution is successful but the output from our last statement is still not on our screen which means the string and of the program must be saved into the file right let's check here is the file let's see if the output from our last Python print statement is saved into it or not and here is a string from our last Python print statement which we don't want now the question here is if saving sis not STD out into sister STD out is not the solution than what to restore the default state of the standard output stream you need to follow a process that process is to save the default state which is sis not STD out into a variable and then assigning it to the standard output stream let me show you how let's create a variable and named it restore point make sure that you must create this variable before the statement which contains the Python open function call which in our case is the second statement now let's save the default state into this variable next we have to assign this variable into sustad STD out that's all you have to do to restore the default behavior of the standard output stream let's execute and see if we are getting the output in the desired format or not the output from all the first four Python print statements are saved into the file but the output of the last print statement is here on the screen which shows that we have successfully restored the standard output stream to its default behavior that is how we work with default output stream in Python if you have watched all the three tutorials of Python print statement then congratulations you have mastered it if you still have any problem feel free to leave a comment down below you can also send me a message on facebook which is facebook.com slash the rebellion right now that's it for today thanks for watching this is Manish from rebellion right calm
Original Description
Python Tutorial on how to automatically redirect the output stream to save the output of Python Print into a file. Enjoy the python Course with easy Examples in 2019 by Manish Sharma
------------------------------------------------------------------------
►►►LINKS◄◄◄
Blog: http://www.rebellionrider.com/automatic-redirection-of-python-print/
Python Open Function:http://www.rebellionrider.com/python-open-file-function/
Previous Tutorial
► Py Tut 20: https://youtu.be/y-o0R6xuCJU
-------------------------------------------------------------------------
►Make sure you SUBSCRIBE and be the 1st one to see my videos!
-------------------------------------------------------------------------
►►►Find me on Social Media◄◄◄
Follow What I am up to as it happens on
https://www.facebook.com/TheRebellionRider/
https://twitter.com/rebellionrider
http://instagram.com/rebellionrider
https://plus.google.com/+Rebellionrider
http://in.linkedin.com/in/mannbhardwaj/
You can also Email me at
for E-mail address please check the About section
Please please LIKE and SHARE my videos it makes me happy.
Thanks for liking, commenting, sharing and watching more of our videos
This is Manish from RebellionRider.com
♥ I LOVE ALL MY VIEWERS AND SUBSCRIBERS
#PythonWithRebellionRider #PythonProgramming #Python3
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Manish Sharma · Manish Sharma · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Oracle Database tutorials 1: How to install Oracle Database 11g on windows 7
Manish Sharma
Oracle Database tutorials 2:How To install SQL Developer on windows 7
Manish Sharma
Oracle Database tutorials 3:How to enable Line numbers in SQL Developer.
Manish Sharma
Oracle Database tutorials 4: database connectivity using SQL developer and command prompt
Manish Sharma
Oracle Database tutorials 5: how to Fetch Data using SELECT - SQL statement by Manish Sharma
Manish Sharma
Oracle Database11g tutorials 6 | | How to use Concatenation operator, character String
Manish Sharma
Oracle Database11g tutorials 7 | |SQL DISTINCT keyword || SQL tutorials
Manish Sharma
Canon EOS 600D 2 lens kit/Canon rebell EOS T3i 2 lens kit Unboxing
Manish Sharma
First look: ORACLE CERTIFIED ASSOCIATE (OCA) CERTIFICATE - ORACLE DATABASE ADMINISTRATOR
Manish Sharma
Oracle Database11g tutorials 8 || SQL DISTINCT with multiple columns |SQL Distinct with Two columns
Manish Sharma
Oracle Database11g tutorials 9 || What is archive log mode and how to enable archive log mode
Manish Sharma
Oracle Database11g tutorials 10 || SQL Single Row Function (SQL Functions )
Manish Sharma
Oracle Database11g tutorials 11: SQL case manipulation function in Oracle Database
Manish Sharma
how to add channel trailer and section on your youtube channel 2014
Manish Sharma
Oracle Database11g tutorials 12 || SQL Concat Function - SQL character manipulation function
Manish Sharma
Oracle Database11g tutorials 13 || SQL substr function / SQL substring function
Manish Sharma
Oracle Database11g tutorials 14 : How to CREATE TABLE using sql developer and command prompt
Manish Sharma
SQL tutorials 15 || How To CREATE TABLE using enterprise manager 11g
Manish Sharma
Oracle Database11g tutorials 16: How to uninstall oracle 11g from windows 7 64 bit
Manish Sharma
ORACLE CERTIFIED PROFESSIONAL(OCP) CERTIFICATE First look - ORACLE DATABASE ADMINISTRATOR
Manish Sharma
Plantronics audio 655 USB headset with Mic Unboxing and Review and Plantronics audio 655 Mic test
Manish Sharma
SQL tutorials 17: SQL Primary Key constraint, Drop primary Key
Manish Sharma
SQL tutorials 18: SQL Foreign Key Constraint By Manish Sharma
Manish Sharma
SQL tutorial 19: ON DELETE SET NULL clause of Foreign Key By Manish Sharma (RebellionRider)
Manish Sharma
SQL tutorials 20: On Delete Cascade Foreign Key By Manish Sharma (RebellionRider)
Manish Sharma
SQL tutorial 21: How To Rename Table in SQL using ALTER TABLE statement By Manish Sharma
Manish Sharma
SQL tutorial 22: How to Add / Delete column from an existing table using alter table
Manish Sharma
SQL tutorial 23: Rename and Modify Column Using Alter Table By Manish Sharma (RebellionRider)
Manish Sharma
SQL tutorial 24 :SQLJoins- Natural Join With ON and USING clause By Manish/Rebellionrider
Manish Sharma
Oracle Database11g tutorials 25: How to install Oracle Database 11g Express Edition R2 on Windows 7
Manish Sharma
SQL tutorial 26: Introduction to SQL Joins in Oracle Database
Manish Sharma
Vidcon 2014 YouTube Fan Funding: How to enable fan funding on YouTube Channel
Manish Sharma
SQL tutorial 27: Right Outer Join in SQL by Manish Sharma for RebellionRider
Manish Sharma
SQL tutorial 28: Left Outer Join By Manish Sharma / RebellionRider
Manish Sharma
SQL tutorial 29: Full Outer Join with example By Manish Sharma/ RebellionRider
Manish Sharma
SQL tutorial 30: Inner Join In SQL by Manish Sharma/RebellionRider
Manish Sharma
SQL tutorial 31 : SQL Cross Join In Oracle Database By Manish Sharma from RebellionRider
Manish Sharma
SQL tutorial 32: How To Insert Data into a Table Using SQL Developer
Manish Sharma
SQL tutorial 33:How To Insert Data into a Table Using SQL INSERT INTO dml statement
Manish Sharma
SQL tutorial 34: How to copy /Insert data into a table from another table using INSERT INTO SELECT
Manish Sharma
SQL tutorial 35: DELETE and TRUNCATE how to delete data from a table
Manish Sharma
SQL tutorial 36: how to create database using database configuration assistant DBCA
Manish Sharma
SQL tutorial 37: How to create NEW USER account using Create User statement in Oracle database
Manish Sharma
SQL tutorial 38: How to create user using SQL Developer in Oracle database
Manish Sharma
SQL tutorial 39: How to create user in oracle using Enterprise Manager
Manish Sharma
SQL tutorial 40: DBA Trick, How to drop a user when it is connected to the database
Manish Sharma
Motorola Moto G 2nd Generation / G2 Unboxing and Review
Manish Sharma
SQL tutorial 41: How to UNLOCK USER in oracle Database
Manish Sharma
SQL tutorial 42: How to Unlock user using SQL Developer By Manish Sharma RebellionRider
Manish Sharma
SQL tutorial 43: How to create an EXTERNAL USER in oracle database By Manish Sharma RebellionRider
Manish Sharma
SQL tutorial 44: How to import data from Microsoft Excel to Oracle Database using SQL Developer
Manish Sharma
SQL tutorial 45: Introduction to user Privileges in Oracle Database By Manish Sharma RebellionRider
Manish Sharma
SQL tutorial 46: What are System Privileges & How To Grant them using Data Control Language
Manish Sharma
SQL tutorial 47: How to Grant Object Privileges With Grant Option in Oracle Database
Manish Sharma
SQL tutorial 48: How to create Roles in Oracle Database
Manish Sharma
SQL tutorial 49: CASE - Simple Case Expression in Oracle Database (1/2)
Manish Sharma
SQL tutorial 50: CASE - Searched Case Expression In Oracle (2/2)
Manish Sharma
SQL tutorial 51: DECODE function in Oracle Database By Manish Sharma (RebellionRider)
Manish Sharma
Oracle Database Tutorial 52 : Data Pump expdp - How to Export full database using expdp
Manish Sharma
Oracle Database Tutorial 53 : Data pump expdp - How to Export tablespace in Oracle Database
Manish Sharma
More on: Python for Data
View skill →
🎓
Tutor Explanation
DeepCamp AI