Skip to main content


Showing posts with the label Python

Getting The Terminal Size In Python

Many times our output doesn’t get fit into the terminal of default size. In that case we have to pull it accordingly and make the size as per our need. Image: Akshay Chauhan on Unsplash In this article, I’ll show you those few lines of Python code using which you can get the size of your terminal programmatically.  Now, before resizing our output we need to know, how to read the terminal size. There are possibly many ways to get the terminal size, i.e. reading environment variables, making low level system calls, etc. In this article, I’ll show you one of the easiest and simplest way to get the terminal size. import os s = os . get_terminal_size ( ) print ( s . columns , s . lines ) Python Copy You can also watch the video recording of this article on my YouTube channel  named Shweta Lodha .

Get Your Horoscope Using Python

If you are a person who believes in horoscopes and also know Python, then this article is for you. Horoscope is a way to forecast future. In this article, I’ll show you how to get your horoscope based on your zodiac sign.  Required Packages Beautiful soup: We will use this to extract data out of HTML and can be installed as shown below: : pip install bs4 Python Copy Requests: We will use this to make HTTP call and can be installed as shown below: : pip install requests Python Copy Website For Reading Horoscope For this article, I’m using a website named for extracting horoscope information and it looks like this: From above page, you can select your zodiac sign and select the day. Doing this will generate an URL similar to this: In this URL, tomorrow represents the day for which I’m looking for horoscope and 9 represents the zodiac sign.  Yes, you guessed it correct. I

Get User Name And Password At Runtime Using Python

It is quite common that you need to grab a logged in user name and ask for the password from a user on an application launch. The biggest question that comes here is — Can we grab password from user in plain text? Can password input be in plain text which can be seen while entering? Of course not. As a password is considered as one of the most sensitive data, it can not be exposed so lightly. Due to its sensitivity, either user input has to be masked or it had taken in invisible form. Let’s learn about it in more detail. Like every other Python application, here also we need to import the required module/package and that is getpass . Required Package getpass allows us to prompt for password without having the password displayed on screen or on user’s terminal. Below is the command to install getpass : pip install getpass Python Copy Read User Name getpass has a function named getuser() , which gives us the login name of a currently logged-in user. userName = getpass . getuser ( ) Pyth

Search For A File Using Python

If you are a coder, then surely you must have came across a requirement wherein you need to search for a file in a given directory. Now, this directory could be any directory — it could be your immediate parent directory or it could be a grand grand parent. In this article, I’ll explain you about how can you search for a file in a given directory using Python. Required Package To perform file search, we only need a package named os and this can be imported using below line of code: import os Python Copy Code To Search A File Here are the few lines of code to perform this task: rootDir = “C : \\Py_Channel” fileToSearch = “activate . bat” for relPath , dirs , files in os . walk ( rootDir ) : if ( fileToSearch in files ) : fullPath = os . path . join ( rootDir , relPath , fileToSearch ) print ( fullPath ) Python Copy In above code, rootDir is the top-level directory in which we want to perform search and fileToSearch is holding the file name, we want to search. os

Multiple Ways To Get Current Working Directory In Python

If you are a programmer, you must have come across a requirement wherein you need to access the current working directory. The current working directory is the folder where your application is running from. In this article, I’ll show you three different ways to get current working directory using Python. Using os.getcwd import os w_dir = os . getcwd ( ) print ( w_dir ) Python Copy Using pathlib.Path.cwd from pathlib import Path work_dir = Path . cwd ( ) print ( work_dir ) Python Copy Using os.path import os print ( os . path . dirname ( os . path . normpath ( __file__ ) ) ) Python Copy Here, the __file__ is a special Python build-in variable which contains the path to the currently running script. The os.path.dirname returns the directory name of the given path and the normpath(...) normalizes a path name by collapsing redundant separators. I hope you enjoyed reading this article. You can also check out the recording of this article on my YouTube channel named Shweta Lodha

Extracting News From Google Using Python

The situation, wherein you are running out of time but still do not want to miss your doze of daily news. Then this article is for you. When it comes to reading news, not everyone is interested in every topic. Few may be interested in sports, others may be interested in either politics or spiritual news or something else. So, based on individual interest, one would like to go and spend time for reading. In this article, I’ll guide on how you can create your own Python utility to extract top headlines from google news based on the user’s interest. User can provide topic of his choice and links of top headlines would be displayed. Import Required Packages In order to start with Python, the very first demand is to grab all the required packages. Here we need two packages, one for extracting news and another one is for handling data. Let’s go ahead and install both the packages: pip install GoogleNews pip install pandas Once these are installed, one

Getting Started With SQLite Database in Python

In this article I'll walk you through the process of using SQLite database, which is an in-built database for Python. We will start by creating an empty database, we will create an empty table, we will insert some data into it and then finally we will try to retrieve our data back. Let's start by importing some required packages Import Required Packages import sqlite3 as sql Create Database We will start by creating an empty database named Sample.db using below line of code: connection = sql.connect("SampleDB.db") Above statement will create the database if not already present and then connect it. Create Table Once the database is created, next thing we need to do is to create an empty table named Student , which will have 3 columns named Id, StudentName and City. Below is the query to create a table named Student: connection.execute( " CREATE TABLE Student(Id INTEGER NOT NULL PRIMARY KE

Screen Capture Using Python

Nowadays, screen capturing is one of the common tasks which most of us are doing in our day-to-day routine and there are instances where in we need to automate this task. In this article, I'll explain you about how can we automate screen capturing using Python. To get started, first of all we need to grab all the required packages and install them. Required packages To automate screenshot capturing, we need to install a package named pyautogui and this can be installed using pip as shown below: pip install pyautogui     Import packages Once required packages are installed, we need to import them into our code. Here we need to import two packages - one for screenshot capturing and another one is for timer as we need to capture our screen after certain seconds of our application launch. Here is the code: import pyautogui import time Implementation  Now we have all the packages imported, we can go ahead and make a call to required function. We will start by calling a function screen