Reputation: 2249
I have some files in a directory without any extension and I was wondering how to search for this files out and add .txt as an extension to them?
Is there a better way to do this?
for file in found:
fileExt = os.path.splitext(file)[-1]
if '' == fileExt:
print 'Found No Ext %s' %file
Upvotes: 1
Views: 2772
Reputation: 287835
You can simply list all files in the directory and then rename them:
dirname = '/some/directory'
for f in os.listdir(dirname):
path = os.path.join(dirname, f)
if not os.path.isfile(path):
continue # A directory or some other weird object
if not os.path.splitext(f)[1]:
os.rename(path, path + '.txt')
Upvotes: 3