Skip to main content

Posts

Python Package

  Python Package We don't usually store all of our files in our computer in the same location. We use a well-organized hierarchy of directories for easier access. Similar files are kept in the same directory, for example, we may keep all the songs in the "music" directory. Analogous to this, Python has packages for directories and modules for files. As our application program grows larger in size with a lot of modules, we place similar modules in one package and different modules in different packages. This makes a project (program) easy to manage and conceptually clear. Similar, as a directory can contain sub-directories and files, a Python package can have sub-packages and modules. A directory must contain a file named __init__.py in order for Python to consider it as a package. This file can be left empty but we generally place the initialization code for that package in this file. Importing module from a package We can import modules from packages using the dot (....
Recent posts

Python Directory | Python File Handling

  Python Directory and Files Management If there are a large number of files to handle in your Python program, you can arrange your code within different directories to make things more manageable. A directory or folder is a collection of files and sub directories. Python has the os module, which provides us with many useful methods to work with directories (and files as well). Get Current Directory We can get the present working directory using the getcwd() method. This method returns the current working directory in the form of a string. We can also use the getcwdb() method to get it as bytes object. >>> import os >>> os.getcwd() 'C:\\Program Files\\PyScripter' >>> os.getcwdb() b'C:\\Program Files\\PyScripter' The extra backslash implies escape sequence. The print() function will render this properly. >>> print(os.getcwd()) C:\Program Files\PyScripter Changing Directory We can change the current working directory using the chdir...

Python File Operation | Learn python

  Python File I/O File is a named location on disk to store related information. It is used to permanently store data in a non-volatile memory (e.g. hard disk). Since, random access memory (RAM) is volatile which loses its data when computer is turned off, we use files for future use of the data. When we want to read from or write to a file we need to open it first. When we are done, it needs to be closed, so that resources that are tied with the file are freed. Hence, in Python, a file operation takes place in the following order. Open a file Read or write (perform operation) Close the file Opening a file Python has a built-in function open() to open a file. This function returns a file object, also called a handle, as it is used to read or modify the file accordingly. >>> f = open( "test.txt" ) # open file in current directory >>> f = open( "C:/Python33/README.txt" ) # specifying full path We can specify the mode while opening a file. In mod...

Python Exception | Learn python

  Python Errors and Built-in Exceptions When writing a program, we, more often than not, will encounter errors. Error caused by not following the proper structure (syntax) of the language is called syntax error or parsing error. >>> if a < 3 File " " , line 1 if a < 3 ^ SyntaxError: invalid syntax Ref: Learn python:Python tutorial app App by Avrram piperidas Most Popular Course:Data Science of Harvard, MIT, IBM....  John Academy: 97% Off on Popular Online Courses Amazon Best Seller in Home and Kitchen Amazon Best Seller in Office Products Amazon Best Seller in Baby Products We can notice here that a colon is missing in the if statement. Errors can also occur at runtime and these are called exceptions. They occur, for example, when a file we try to open does not exist (FileNotFoundError), dividing a number by zero (ZeroDivisionError), module we try to import is not found (ImportError) etc. Whenever these type of runtime error occur, Python ...

Python Exception Handling | Learn python

  Python Exception Handling - Try, Except and Finally Python has many built-in exceptions which forces your program to output an error when something in it goes wrong. When these exceptions occur, it causes the current process to stop and passes it to the calling process until it is handled. If not handled, our program will crash. For example, if function A calls function B which in turn calls function C and an exception occurs in function C. If it is not handled in C, the exception passes to B and then to A. If never handled, an error message is spit out and our program come to a sudden, unexpected halt. Catching Exceptions in Python In Python, exceptions can be handled using a try statement. A critical operation which can raise exception is placed inside the try clause and the code that handles exception is written in except clause. It is up to us, what operations we perform once we have caught the exception. Here is a simple example. # import module sys to get the type of except...