Skip to main content

Posts

Showing posts with the label YouTube

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

Effective ways to sort Python iterables

The traditional way to sort iterables is by using loops, like for loop or while loop. As part of this article, I am not going to cover this traditional way, rather I will be focusing on a better way to implement sorting. And that better way is by using sorted function . Let's take a look at below list holding some numbers: numbers = [ 2 , 3 , 6 , 4 , 9 , 1 ] Python Copy To sort this list, we just need to call a function as shown below:  numbers = sorted ( numbers ) Python Copy To sort this list in reverse order, we need to pass an additional parameter: numbers = sorted ( numbers , reverse = True ) Python Copy Apart from this simple case, we can also handle some complex scenarios wherein we have a list containing key-value pairs as shown: studentDetails = [ { “Name” : “Nick” , “Marks” : 23 } , { “Name” : “Roy” , “Marks” : 2 } , { “Name” : “Honey” , “Marks” : 45 } ] Python Copy Now, say you want to sort studentDetails based on Marks. So, the only code you n

Create Word Art From An Image Using Python

If you are Python developer then you may be fully convinced that we can do very cool things using Python in very easy way. Today I’m going to show you a very beautiful usage of Python, in which we will see how we can generate a word art of any given image. You can pick an image of your pet, you can pick an image of a natural scenery, you can pick an image of human, basically you can pick anything as your image.  Once the image is selected, it is just the matter of few lines of Python code. Package To generate word art we need to install a package named pywhatkit . If you are using pip to install your Python packages, then here is the syntax: pip install pywhatkit Code Once the package is installed, you need to import the package and call the function named image_to_ascii_art(…) . This function will take two parameters — first one is the image file path and second parameter is the name for output file. Here are the two lines of code: import pywhatkit pywhatkit.image_to_ascii_art(“Wolf.p

Keep Your Screen Active Forever Using Python

If your boss is very workaholic and you are not working from office then you must have heard one of these statements from your boss: Why were you not at your desk  Why your status was away in Teams/Skype/etc I couldn't see you really putting 8 hours at work, blah blah blah. source: launchworkplaces.com And all these statements are coming because your machine was inactive and that change your communicator status to Away . Well, so today we are going to solve this problem where in we will not let our communicator status change by its own. How can we do that? Answer is very simple. This problem can be solved, if our machine is active, if we are continuously (or in few mins) pressing some keys or moving our mouse.  Here we will use Python to write just few lines of code to perform this magic.  Python package We will use Python package named pyautogui as it allows us to automate mouse and keyboard interactions. It works very well on Windows, Mac as well as Linux. You can install this pa

Create Candlestick Charts For Stocks Using Yahoo Finance

In this article, we will see how to create candlestick chart with historical data for any given symbol using open financial API named Yahoo Finance and Python. We will start by pushing data in a CSV file and then we will use Plotly to create the candlestick chart in Python. Introduction to candlestick chart This is one of the heavily used charts you may have seen multiple times while dealing with stock market dashboards. This is very much used by traders as it provides price movement based on the past patterns. One candle represents 4 points: Open, Close, High, Low as shown below:  In above figure, first candle is filled which depicts that open price was higher than close price. Similarly, second candle depicts that close price was higher than open price. Here do not focus on the color of candle as it can be configured based on your favorite color, based on the dashboard you are using.  In general,  bullish candlestick (close price > open price) is represented by green color and bea

Top 5 Visual Studio Code Extensions for Azure

In my one of the recent posts, I mentioned about top five Visual Studio Code extensions and after that I received lot many feedbacks asking my top recommendation on Azure specific extensions. Hence, this article is here. When it comes to Azure, there are lot many extensions you can find. Here is the gist of that: Above list is not limited to what I've shown above, there is much more than this. Let's quickly jump on to my top five favorite extensions specific to Azure: Azure Tools This is my most favorite one as it handles lot many things. It includes long list of extensions which are very much useful to interact with Azure. It allows you to host your web sites, it allows very seamless interaction between MongoDB and Cosmos DB in terms of creating databases and writing scripts, you can manager your virtual machines, view all your Azure resource groups, you can deal with docker images and many more.  Here is the list extensions included in it: Docker Azure Functions Azure Resourc

Implementing Dependency Injection In Azure Functions

This article talks about how we can use dependency injection in Azure Functions. Dependency injection is a very well known design pattern that is used to implement IoC as provides you a facility to segregate object creation logic from its usage.  Prerequisites And Tools Used In order to implement dependency injection in Azure Function App, you need an active Azure subscription, Visual Studio (I’m using Visual Studio 2019), and working knowledge of the C# language.  Create Function App in Visual Studio The first step is to create a new project in Visual Studio of type Azure Functions: and select Http Trigger as shown below:  Add Classes For Dependency Injection Next, we need to add the service (classes) which we want to inject. For simplicity, we will create an interface and a class implementing that interface. This interface will have only one method named GetCurrentTime() and it will provide current time to the caller. Here are the definitions: public interface ICurrentTimeProvider {