Skip to main content

Posts

Showing posts from June, 2022

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 https://www.horoscope.com 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: https://www.horoscope.com/us/horoscopes/general/horoscope-general-daily-tomorrow.aspx?sign=9 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