python remove extension from filename code example
Example 1: python delete file with extension
import os
dir_name = "/Users/ben/downloads/"
test = os.listdir(dir_name)
for item in test:
if item.endswith(".zip"):
os.remove(os.path.join(dir_name, item))
Example 2: split filename and extension python
>>> import os
>>> filename, file_extension = os.path.splitext('/path/to/somefile.ext')
>>> filename
'/path/to/somefile'
>>> file_extension
'.ext'
Example 3: python get file extension from path
import os.path
extension = os.path.splitext(filename)[1]
Example 4: python code to remove file extension
import os
print os.path.splitext("sample.txt")[0]
Example 5: strip file extension python
import os
print(os.path.splitext("/path/to/some/file.txt")[0])