Python

How to Sort Pandas Series (examples included)

Here is the syntax to sort Pandas Series: (1) Sort Pandas Series in an ascending order: import pandas as pd mySeries = pd.Series([‘Item1′,’Item2′,’Item3’,…]) sortedSeries = mySeries.sort_values(ascending=True) print(sortedSeries) (2) Sort Pandas Series in a descending order. In this case, simply set ascending=False: import pandas as pd mySeries = pd.Series([‘Item1′,’Item2′,’Item3’,…]) sortedSeries = mySeries.sort_values(ascending=False) print(sortedSeries) In this guide, …

How to Sort Pandas Series (examples included) Read More »

How to Get the File Size using Python

You can use the syntax below in order to get the file size (in bytes) using Python: import os.path file_path = r’path where the file is stored\file name.file extension’ file_size = os.path.getsize(file_path) print(file_size) Optionally, you can use the following code to get the size in bytes, kilobytes, megabytes and gigabytes: import os.path file_path = r’path …

How to Get the File Size using Python 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: import os.path my_path = r’path where the file is stored\file name.file extension’ ext = os.path.splitext(my_path)[1] print(ext) (2) Extract the file extension without the dot: import os.path my_path = r’path where the file is stored\file name.file extension’ …

How to Extract the File Extension using Python 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: 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 time, …

How to Get the Modified Time of a File using Python Read More »

Check if a File or Directory Exists using Python

You can use the following templates to check if a file or directory exists using Python: (1) Check if a file exists using os.path.isfile: import os.path file_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: import os.path directory_exists = os.path.isdir(r’path of directory’) print(directory_exists) Steps to …

Check if a File or Directory Exists using Python Read More »