How to Get the File Size using Python

To get the file size (in bytes) using Python: Copy import os file_path = r”path where the file is stored\file_name.file_extension” file_size = os.path.getsize(file_path) print(file_size) Optionally, to get the size in bytes, kilobytes, megabytes and gigabytes: Copy import os file_path = r”path where the file is stored\file_name.file_extension” file_size = os.path.getsize(file_path) print(“File size in Bytes: ” + … Read more

How to Extract the File Extension using Python

Here are 3 ways to extract the file extension using Python: (1) Extract the file extension with the dot: Copy import os.path file_path = r”path where the file is stored\file_name.file_extension” file_extension = os.path.splitext(file_path)[1] print(file_extension) (2) Extract the file extension without the dot: Copy import os.path file_path = r”path where the file is stored\file_name.file_extension” file_extension = … Read more

Get the first N rows in Pandas DataFrame

You can use df.head() to get the first N rows in Pandas DataFrame. For example, if you need the first 4 rows, then use: Copy df.head(4) Alternatively, you can specify a negative number within the brackets to get all the rows, excluding the last N rows. For example, you can use the following syntax to … Read more

How to get the Latest File in a Folder using Python

In this guide, you’ll see how to get the latest file in a folder using Python. To start, here is a template that you may use to get the latest file: Copy import glob import os.path folder_path = r’path where your files are located’ file_type = r’\*type’ files = glob.glob(folder_path + file_type) max_file = max(files, … Read more

Convert JSON to TEXT File using Python

The following template can be used to convert a JSON file to a text file using Python: Copy import pandas as pddf = pd.read_json(r”Path where the JSON file is saved\File Name.json”)df.to_csv(r”Path where the new TEXT file will be stored\New File Name.txt”, index=False) Steps to Convert JSON to TEXT using Python Step 1: Prepare the JSON … Read more

How to Get the Modified Time of a File using Python

You can use the following template to get the modified time of a file using Python: Copy import os.path modified_time = os.path.getmtime(r’path where the file is stored\file name.file extension’) print(modified_time) The above approach expresses the time in seconds since the epoch. If you wish to convert the time to a string that represents a local … Read more

Check if a File or Directory Exists using Python

You can use the following templates to check if a file or a directory exists using Python: (1) Check if a file exists using os.path.isfile: Copy import os.pathfile_exists = os.path.isfile(r”path where the file is stored\file_name.file_extension”)print(file_exists) (2) Check if a directory exists using os.path.isdir: Copy import os.pathdirectory_exists = os.path.isdir(r”path of directory”)print(directory_exists) Steps to Check if a … Read more

How to Count NaN values in Pandas DataFrame

You can use the following syntax to count NaN values in Pandas DataFrame: (1) Count NaN values under a single DataFrame column: Copy df[‘column name’].isna().sum() (2) Count NaN values under an entire DataFrame: Copy df.isna().sum().sum() (3) Count NaN values across a single DataFrame row: Copy df.loc[[index value]].isna().sum().sum() Let’s see how to apply each of the … Read more

Drop Columns with NaN Values in Pandas DataFrame

Here are 2 ways to drop columns with NaN values in Pandas DataFrame: (1) Drop any column that contains at least one NaN: Copy df.dropna(axis=’columns’, inplace=True) (2) Drop column/s where ALL the values are NaN: Copy df.dropna(axis=’columns’, how=’all’, inplace=True) The Example For demonstration purposes, let’s create a DataFrame with 5 columns, where: Here is the … Read more

Convert Floats to Strings in Pandas DataFrame

Here are 3 approaches to convert floats to strings in Pandas DataFrame for: (1) A single DataFrame column using astype(str): Copy df[“DataFrame Column”] = df[“DataFrame Column”].astype(str) (2) A single DataFrame column using apply(str): Copy df[“DataFrame Column”] = df[“DataFrame Column”].apply(str) (3) An entire DataFrame using: Copy df = df.astype(str) 3 Approaches to Convert Floats to Strings … Read more