python windows 文件重命名

Windows系统,在同一个文件夹内,无法命名一个相同文件名的文件。linux可以直接重命名,并会替换掉之前的文件。

所以要先删除文件后在重命名,并且要获取文件路径。

import os
from nt import chdir
def renameR(new_file,old_file):#删除文件,并且用删除文件的文件名,重命名另外一个文件
    os.remove(old_file)
    chdir(os.path.dirname(new_file))
    os.rename(new_file, old_file)
account_file = 'E:/1.txt'
account_file2 = file('E:/1.txt','w+b')
account_file2.close()
new_f = file('E:/2.txt','w+b')
new_f.close()
new_ff = 'E:/2.txt' 

renameR(account_file, new_ff)

 

 
 

 

 

 

posted @ 2015-12-17 12:34  独孤玖剑  阅读(1351)  评论(0编辑  收藏  举报