find the extension of a csv file in python

To find the extension of a CSV file in Python, you can use the splitext() function from the os.path module. Here is an example:

main.py
import os

filename = 'example.csv'
extension = os.path.splitext(filename)[1]

print(extension) # Output: ".csv"
113 chars
7 lines

In this code snippet, the splitext() function splits the filename into its root and extension parts, and returns them as a tuple. We then access the extension part (which is the second element of the tuple) and assign it to the extension variable.

Note that the extension includes the dot (.) character, so you may want to remove it if you just need the file type (e.g. csv). You can do this using string manipulation techniques such as slicing or the replace() function.

gistlibby LogSnag