python批量重命名文件

来源:互联网 发布:linux ping怎么使用 编辑:程序博客网 时间:2024/05/20 09:23

#!/usr/bin/python
# -*- coding:UTF-8 -*-

import os

def rename_file(path):

for file in os.listdir(path):
if os.path.isfile(os.path.join(path,file)) == True:
if file.find('.json') < 0:
newname = file + '.json'
os.rename(os.path.join(path,file),os.path.join(path,newname))
print file,'ok'
else:
continue
else:
continue


print rename_file('/root/hosts')
0 0