Skip to main content

Posts

Showing posts from May, 2022

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