SaiKrishna Tavva

SaiKrishna Tavva

80 Articles Published

Articles by SaiKrishna Tavva

80 articles

Run a Python program from PHP

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 15-Mar-2026 7K+ Views

In PHP, you can execute Python scripts using built-in functions like exec() or shell_exec(). These functions allow you to run Python programs via the shell and capture their output. exec() Function The exec() function executes commands in the shell and optionally captures the output. It returns only the last line of output by default. Syntax exec(string $command, array &$output = null, int &$return_var = null); shell_exec() Function The shell_exec() function is similar to exec() but captures and returns the entire output of the command as a string, instead of just the last ...

Read More

How to call Python file from within PHP?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 15-Mar-2026 11K+ Views

In PHP, you can execute Python scripts using built-in functions like shell_exec(), exec(), or proc_open(). Each method offers different levels of control over the process. Using shell_exec() The simplest approach − runs a command via the shell and returns the output as a string. Use escapeshellcmd() to sanitize the command ? The square of 4 is 16 Specifying the Python Interpreter If the script lacks a shebang line (#!/usr/bin/env python3), explicitly call the interpreter ? The square of 5 is 25 ...

Read More

How to prepare a Python date object to be inserted into MongoDB?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 14-Mar-2026 2K+ Views

MongoDB stores dates in ISODate format. PyMongo (the official MongoDB driver for Python) directly supports Python's datetime.datetime objects, which are automatically converted to ISODate when inserted. There are three common ways to prepare date objects for MongoDB. Method 1: Current UTC Timestamp Use datetime.datetime.utcnow() to insert the current UTC time ? import datetime from pymongo import MongoClient client = MongoClient() db = client.test_database result = db.objects.insert_one({"last_modified": datetime.datetime.utcnow()}) print("Date Object inserted") Date Object inserted Method 2: Specific Date Use datetime.datetime(year, month, day, hour, minute) for a fixed date ? ...

Read More

How to install a Python package into a different directory using pip?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 01-Sep-2025 22K+ Views

In Python, pip is a standard tool which is used to install third-party packages in our system. By default, pip installs packages into the site-packages directory of the current Python environment but in some cases, such as restricted environments or creating portable apps, we may need to install packages into a different directory. Then we can use the pip's --target option. Syntax Following is the syntax of using the pip command while installing a python package in a different directory - pip install --target The above command tells pip to install the specified package and all its ...

Read More

How to ignore hidden files using os.listdir() in Python?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 01-Sep-2025 7K+ Views

When we are working with directories in Python, a clutter of hidden files can be created. Hidden files are created with a dot (.) on Unix-like operating systems and we can filter them easily. In this article, we will explore different methods to ignore hidden files using the os module and pathlib. Using List Comprehension with os.listdir() In Python, when we use the os.listdir() method the hidden files i.e., the files those are starting with a dot . are also included in the result. To ignore those hidden files and list only visible files we need to pass a condition ...

Read More

How to delete a Python directory effectively?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 01-Sep-2025 1K+ Views

When we are working with Python files or directories, we may often need to delete them to perform tasks such as cleaning up temporary files. Deleting directories isn't an easy task when compared with deleting files. In this article, we will explore all the different methods to delete a Python directory. Using shutil.rmtree() for Recursive Deletion The shutil module in Python has the rmtree()function, which is used to recursively delete a directory and all its contents. This function takes the directory path as the input parameter. Example Following is the example which uses the function shutil.rmtree() to delete a directory ...

Read More

How to get specific nodes in xml file in Python?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 01-Sep-2025 10K+ Views

XML is abbrivated as Extensible Markup Language which is used format to represent a structured data. It's is useful when we are exchanging information between two systems. In Python, the xml.etree.ElementTree module helps us to read and work with XML data. In this article, we will explore how to extract specific parts of an XML file using this library. Introduction to XML and ElementTree When we are working with XML files in Python, the xml.etree.ElementTree module is used for parsing and navigating XML structures. It reads an XML document and builds a tree of elements from it by allowing us ...

Read More

How do you get a directory listing sorted by creation date in Python?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 01-Sep-2025 12K+ Views

When working with files in Python, it's often useful to sort them based on their creation time. For example, to find the most recently added file or to process files in the order they were created then we need sort them by the creation date. Python provides built-in methods such as os and os.path which makes it easy to retrieve file metadata and sort directory contents accordingly. In this article, we will explore all the different method to sort the directories based on the creation date in Python - Using os.listdir() with sorted() and os.path.getctime() ...

Read More

How to copy certain files from one folder to another using Python?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 01-Sep-2025 4K+ Views

When we are working with files in Python we may need to copy only a certain type of file. Python makes this task simple and flexible by using built-in modules such as os and shutil and pathlib. Copying specific files using shutil and os modules When we want to copy only certain files such as all .txt files from one folder to another in Python then we can use the shutil and os modules. The shutil module is used to perform high-level operations on files such as file copying, moving, archiving and removing directories whereas os module is used to ...

Read More

How to merge two Python dictionaries in a single expression?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 01-Sep-2025 353 Views

In Python, a Dictionary is a unordered and muttable data structure which stores data as a key-value pair. In some cases, we may need to merge two dictionaries into a single dictionary by using a single expression, in such scenarios python provides different methods. Using | Merge Operator The | merge operator in Python is used to merge multiple dictionaries into one with a single expression and returns the combined dictionary as a new dictionary. If both dictionaries have the same key, then the value from the dictionary on the right side of the operator will be replaced with the ...

Read More
Showing 1–10 of 80 articles
« Prev 1 2 3 4 5 8 Next »
Advertisements