Use Python to get the File Paths within a Folder

Here are 3 ways to get the file paths that match specific patterns within a folder: (1) Fetch all CSV files within the specified folder Copy import glob folder_path = r”C:\Users\Ron\Desktop\Test” csv_files = glob.glob(folder_path + r”\*.csv”) print(csv_files) Where: The result is a list containing the full paths of 3 CSV files inside the “Test” folder: … Read more

How to Write Data to a JSON File using Python

Here are 3 ways to write data to a json file using Python: (1) Single dictionary to a json file: Copy import json data = { “product”: “Computer”, “price”: 1300, “brand”: “A” } file_path = r”path to save the json file\file_name.json” with open(file_path, “w”) as file: json.dump(data, file, indent=4) Where: The resulted json file: (2) … Read more

Write List of Lists to CSV File in Python

To write a List of Lists to a CSV file in Python: Copy import csv data = [ [“value_1”, “value_2”, “value_3”], [“value_4”, “value_5”, “value_6”], [“value_7”, “value_8”, “value_9″] ] file_path = r”path to store the file\file_name.csv” with open(file_path, “w”, newline=””) as file: csv_writer = csv.writer(file) csv_writer.writerows(data) Example of Writing a List of Lists to a CSV … Read more

How to Resize an Image in Python

The PIL library can be used to resize an image in Python. In this short guide, you’ll see the full steps to resize your image. Steps to Resize an Image in Python (1) To start, install the PIL library using the following command: Copy pip install Pillow (2) Use the script below to resize your … Read more

Install the Matplotlib Package in Python

To install the Matplotlib package in Python: Copy pip install matplotlib Note that the above command would only work if you already added Python to the Path. Otherwise, check the steps below to install the Matplotlib package in Python. Steps to Install the Matplotlib Package in Python (1) Locate the Python Scripts folder using these steps: (2) … Read more

Fix the Unicodeescape Error when Specifying a Path in Python

When specifying a file path in Python, you may face the following unicodeescape error if the path contains backslashes: SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated \UXXXXXXXX escape In this short guide, you’ll see two ways to overcome this error. Two ways to fix the unicodeescape error (1) Place “r” … Read more

Solve PIP is Not Recognized as an Internal or External Command

In this short guide, you’ll see how to solve the following error: ‘pip’ is not recognized as an internal or external command, operable program or batch file. Steps to Solve PIP is Not Recognized as an Internal or External Command Step 1: Download a recent version of Python Download a recent version of Python by … Read more

How to Install the NumPy Package in Python

You can install the NumPy package by typing this command in the Command Prompt or terminal: pip install numpy Note that the above command would only work if you added Python to the Path. Otherwise, check the steps below to install the NumPy package in Python. Steps to Install NumPy on Windows (1) Locate the … Read more

Use Configparser to Extract Values from an INI File in Python

In this short guide, you’ll see how to use configparser to extract values from an INI file. To start, here is a simplified template that you may use: Copy import configparser # Create a ConfigParser object config = configparser.ConfigParser() # Read the INI file config.read(‘config.ini’) # Access the values section_name = ‘SectionName’ option_name = ‘OptionName’ … Read more

How to Install the Pandas Package in Python

You can install the Pandas package by typing this command in the Command Prompt or terminal: pip install pandas Note that the above method would only work if you already added Python to the Path. Otherwise, check the steps below to install the Pandas package in Python. Steps to Install the Pandas Package in Python … Read more