How to Append Data to a JSON File in Python? [+Video]

Problem Formulation Given a JSON object stored in a file named “your_file.json” such as a list of dictionaries. πŸ’¬ How to append data such as a new dictionary to it? # File “your_file.json” (BEFORE) [{“alice”: 24, “bob”: 27}] # New entry: {“carl”: 33} # File “your_file.json” (AFTER) [{“alice”: 24, “bob”: 27}, {“carl”: 33}] Method 1: … Read more

How To Read A JSON File With Python

Are you confronted with a JSON file and at a loss to know how to extract wanted information, or you don’t know about JSON but would like to? Today we’ll introduce the JSON format and use Python to access a JSON file. We’ll extract the data we need and we’ll use the Python JSON module … Read more

How to Change the Working Directory in Python

If you have worked on a Python application where you had data in another folder, you would have used a command-line tool like cd to change directories. In this tutorial, we will learn a more Pythonic way of changing directories. Changing directories using the os.chdir function The easiest way to change the working directory in … Read more

How to Open Outlook in Python?

Problem Formulation Given a Windows operating system on which you have the Microsoft Outlook email program installed. How to open Microsoft Outlook using only a function call in your Python script? Solution: os.startfile(‘outlook’) The easiest way to open Outlook on Windows using only a short and concise Python One-Liner is to use the os.startfile(‘outlook’) function … Read more

How to rm -rf in Python to Delete a Directory?

Problem Formulation: How to Remove a Directory in Python? The rm command in Linux removes a specific directory. You can also add the options -r remove the directory recursively -f ignore nonexistent files and arguments and don’t prompt the user to ask for confirmation So, if you run rm -rf my_directory, it’ll forcefully remove my_directory … Read more

How To Run Multiple Python Versions On Windows?

Summary: You can run multiple versions of Python on Windows using one of the following methods: Using entire path to execute the code. Creating A Shortcut or Symbolic Link to the executable files. Using Pylauncher: Use Shebang (#) in The Script Run Pylauncher Command Using Virtual Environments. ➠ Problem Formulation: You might have two versions … Read more

Python open() Function — An 80/20 Guide By Example

Python’s built-in open() function opens a file and returns a file object. The only non-optional argument is a filename as a string of the file to be opened. You can use the file object to access the file content. For example, file_obj.readlines() reads all lines of such a file object. Here’s a minimal example of … Read more