Posts

Showing posts from October, 2021

USING WITH BLOCK TO OPEN FILE

  Using With Block To Open Python Files There is more than one way to open and close a file. The one we have studied till now is using the  open()  and  close()  function. In today's tutorial, we will go through another yet better and straightforward approach of opening and closing files. We will see how we can use, with block to open and close a file, including syntax and benefits.  We will be using f as our file’s object . Opening and closing of files are necessary and crucial steps in file handling. We cannot read, write, or perform any task on a file without opening it first. Everyone is familiar with it because it is the first step in file handling. But what most of us are not familiar with is how vital closing a file is. If we do not close our file after we are done using it, then the file object will keep on consuming processor memory, and also, there will be more chances of exceptions as the file is still open hence, more chances of bugs. To save ou...

Seek(), tell() & More On Python Files

  Seek(), tell() & More On Python Files Python file objects give us many methods and attribute that we can use to analyze a file, including tools to figure out the name of the file associated with the file object, whether it is closed or opened, writable, readable and how it handles errors. We have already discussed the file, its access modes, how to open, close, read, and write files in the previous tutorials. Now it is time to pay attention to some of the most useful and important functions used in file handling. In today’s tutorial, we are going to study why there is a need for tell() and seek() functions and how we can use them?  tell() When we are working with python files, there are several ways to present the output of a program, it could be in human-readable form or binary form, or we use read() function with specified sie to read some data. What if we want to know the position of the file(read/write) pointer. For this purpose, we use  the tell() function . f....

EXERCISE 4 ASTROLOGER STARS

  # pattern printing # input = integer n # boolean variable = true or false #true n =5 #* #** #*** #**** #false n= 5 #**** #*** #** #* print ( "enter a number for pattern" ) i = 0 while i <= 5 : i = i + 1 n = int ( input ()) if n == 0 : print ( "* \n ** \n *** \n ****" , 5 -i, "remaining row" ) break elif n == 1 : print ( "**** \n *** \n ** \n *" , 5 -i, "remaining row" ) break elif n != 0 : 1 print ( "enter 0 or 1 \n " , 5 -i, "remaining row" ) continue

WRITING AND APPENDING TO A FILE

Writing And Appending To A File We have already discussed how to open and read a file in Python, in the last tutorial. If you haven’t seen the video, please go and see that one first. Now we will discuss how to write or insert text to a file and also how we can simultaneously read and write a file. Now, as we know, there are different modes of opening a file, we will cover three of them in this tutorial. Note: f is an object for the file. It is not a method or a special character. You will notice me using f.write() or f.read() or f.close(), in further description or tutorial, but you can use character or word of your own choice instead. MODES PURPOSE “w” mode:   Here “w” stands for write. After opening or creating a file, a function, f.write() is used to insert text into the file. The text is written inside closed parenthesis surrounded by double quotations. There is a certain limitation to the write mode of the opening file that it overrides the existing data into the file. F...

python exercise 3

  # no of guesses 5 # print no of guesses left # No of guesses he took to finish # game over print ( "guess a number" ) i = 0 while (i <= 5 ): i = i + 1 n = int ( input ( "guess number:" )) if n> 18 : print ( "enter smaller number \n no of guesses remain" , 5 -i) elif n< 18 : print ( "enter greater number \n no of guesses remain" , 5 -i) elif n == 18 : print ( 'Guess is right ☺️ ' ) i = i + 1 break if i>= 5 : print ( "game over" )

Open(), Read() & Readline() For Reading File IN PYTHON

  Open(), Read() & Readline() For Reading File As we now have an idea of what files(text or binary) are and their access modes, we are now ready to dive into the discussion of file handling methods. When we want to read or write a file (say on our hard drive), we must first open the file. When we open a file, we are asking the operating system to find the file by name, making sure the file exists. How to open a file ? Python has a built-in open() function to open a file. The syntax of the function is: open ( "filename" , "mode" ) Copy To open a file, we must specify two things, Name of the file and its extension Access mode where we can specify in which mode file has to be opened, it could either be read (r), write (w) or append(a), etc. For more information regarding access modes, refer to the previous tutorial. For Example,  open ( "myfile.txt" ) Copy The file “myfile.txt” will open in "rt" mode as it is the default mode. But the best prac...

PYTHON FILE IO BASIC

  Python File IO Basics You must have noticed that till now we have been learning one new concept per tutorial. For some important concepts like loops we had to allocate two tutorials so we can grasp the concept of both loops (for and while) separately. But now in the case of the file, we have allocated the next five tutorials (excluding the exercise and their solutions). So, from this, you can take a hint that how important file handling is in programming. In this tutorial we are not getting into files in detail, instead, we are discussing the basics of the file and its modes in a theoretical manner. In computer terms, “a file is a resource for saving data and information in computer hardware”. A file is stored in the form of bytes in hardware. A file is opened in the RAM, but it is stored in the hardware because the hardware is non-volatile i.e. it stores its data permanently. On the other hand, RAM is volatile, it loses its data when the system is shut down. Unlike C or C++, fil...

TRY EXCEPT EXCEPTION HANDLING IN PYTHON

  Try Except Exception Handling In Python Before discussing exceptional handling, let us discuss, what an exception is actually. “Exception can be said as an error, that causes a program to crash. Unlike syntax error, it is syntactically correct and occurs mostly due to our negligence” For example, assigning a string value to an int data type variable or dividing a number by zero or also when a name of a variable or a function is not found, an exception occurs. Python has built-in support for dealing with many sorts of exceptions automatically, but we can also define our own. The solution to exception related problems is simple. We just have to alter the normal flow of the program by a bit of code known as an exception handler. This code will save the state of the program up to the point where the exception occurred and will continue the normal flow from the code written outside the area of occurrence of an exception. We can also print the exception by converting it into a string. ...