Adding Extension To Multiple Files (Python3.5)
I have a bunch of files that do not have an extension to them. file needs to be file.txt I have tried different methods (didn't try the complicated ones since I am just learning t
Solution 1:
You need os.rename. But before that,
Check to make sure they aren't folders (thanks, AGN Gazer)
Check to make sure those files don't have extensions already. You can do that with
os.path.splitext.
import os
root = os.getcwd()
for file in os.listdir('.'):
if not os.path.isfile(file):
continue
head, tail = os.path.splitext(file)
if not tail:
src = os.path.join(root, file)
dst = os.path.join(root, file + '.txt')
if not os.path.exists(dst): # check if the file doesn't exist
os.rename(src, dst)
Post a Comment for "Adding Extension To Multiple Files (Python3.5)"