os.rename(src, dst)
src -- 這是在檔案或目錄的實際名稱
dst -- 這是在檔案或目錄的新名稱
# !/usr/bin/python3 import os, sys os.chdir("d:\\tmp") # listing directories print ("The dir is: %s"%os.listdir(os.getcwd())) # renaming directory ''tutorialsdir" os.rename("python3","python2") print ("Successfully renamed.") # listing directories after renaming "python3" print ("the dir is: %s" %os.listdir(os.getcwd()))
The dir is: ['Applicationdocs.docx', 'book.zip', 'foo.txt', 'Java Multiple Inheritance.html', 'Java Multiple Inheritance_files', 'java.ppt', 'Python3'] Successfully renamed. the dir is: ['Applicationdocs.docx', 'book.zip', 'foo.txt', 'Java Multiple Inheritance.html', 'Java Multiple Inheritance_files', 'java.ppt', 'python2']