Python檔案和資料夾處理相關函式:刪除、複製、移動
這裡使用的是Python3.7版本,在Win10系統上執行
有關刪除檔案、資料夾的問題,網上有不少資料,但作為一個新手,感覺很多都介紹的模稜兩可,必須要自己測試過才能知道到底是怎麼回事,因此在這裡簡單介紹一下Python處理檔案和資料夾的函式。
目錄
刪除
一、刪除檔案
os.remove(path)
path表示要刪除的檔案路徑,示例:
import os
os.remove('G:\\test\\test.txt')
解釋:
(1)如果path是一個檔案路徑,執行os.remove()會正常刪除代表的檔案
(2)如果path是一個目錄路徑,執行os.remove()會報錯
二、刪除資料夾
1. 刪除空資料夾
os.rmdir(path)
path表示要刪除的目錄路徑(資料夾路徑),示例:
import os
os.rmdir('G:\\test1\\test2\\test3')
解釋:
(1)如果test3是一個空的資料夾,執行os.rmdir()會刪除test3資料夾
(2)如果test3不是一個空的資料夾,其下有資料夾或檔案,執行os.rmdir()會報錯
2. 遞迴刪除空資料夾
os.removedirs(path)
path表示要刪除的目錄路徑(資料夾路徑),示例:
import os
os.removedirs('G:\\test1\\test2\\test3\\test4')
解釋:
(1)如果test4是一個空資料夾,會從test4開始,利用os.rmdir()逐層刪除空資料夾,直到遇到非空資料夾停止。
若test3存在同級資料夾或者同級檔案,執行os.removedirs()會刪除test4。
若test3不存在同級資料夾或者同級檔案,而test2存在同級資料夾或者同級檔案,會刪除test3、test4。
(2)如果test4不是一個空資料夾,其下有資料夾或檔案,執行os.removedirs()會報錯。
3. 刪除資料夾
shutil.rmtree(path)
path表示要刪除的目錄路徑,示例:
import shutil
shutil.rmtree('G:\\test1\\test2\\test3')
解釋:
不論test3資料夾是空資料夾還是非空資料夾,執行shutil.rmtree()都會刪除test3資料夾
複製
一、複製檔案
1. 複製檔案內容
shutil.copyfile(oldpath, newpath)
oldpath是原檔案路徑,newpath是新檔案路徑,示例:
import shutil
oldpath = 'G:\\test1\\test1.txt'
newpath = 'G:\\test2\\test2.txt'
shutil.copyfile(oldpath, newpath)
解釋:
(1)如果新檔案路徑中test2.txt不存在,會新建test.txt後寫入資料,因此檔案的建立時間、修改時間是新的
(2)如果新檔案路徑中test2.txt存在,則會覆蓋其中的資料,寫入新資料,因此檔案的修改時間是新的
原始碼:
def copyfile(src, dst, *, follow_symlinks=True):
"""Copy data from src to dst.
If follow_symlinks is not set and src is a symbolic link, a new
symlink will be created instead of copying the file it points to.
"""
if _samefile(src, dst):
raise SameFileError("{!r} and {!r} are the same file".format(src, dst))
for fn in [src, dst]:
try:
st = os.stat(fn)
except OSError:
# File most likely does not exist
pass
else:
# XXX What about other special files? (sockets, devices...)
if stat.S_ISFIFO(st.st_mode):
raise SpecialFileError("`%s` is a named pipe" % fn)
if not follow_symlinks and os.path.islink(src):
os.symlink(os.readlink(src), dst)
else:
with open(src, 'rb') as fsrc:
with open(dst, 'wb') as fdst:
copyfileobj(fsrc, fdst)
return dst
2. 複製檔案內容和許可權
shutil.copy(oldpath, newpath)
oldpath是原檔案路徑,newpath可以是新檔案路徑,也可以是資料夾路徑,示例:
import shutil
oldpath = 'G:\\test1\\test1.txt'
newpath1 = 'G:\\test2\\test2.txt'
newpath2 = 'G:\\test2'
shutil.copy(oldpath, newpath1)
shutil.copy(oldpath, newpath2)
解釋:
(1)newpath是新檔案路徑時:
若test2.txt不存在,會新建test2.txt後寫入資料和許可權,因此檔案的建立時間、修改時間是新的
若test2.txt存在,則會覆蓋其中的資料,寫入新資料和許可權,因此檔案的修改時間是新的
(2)newpath是資料夾路徑時:
若路徑下test1.txt不存在,會新建test1.txt後寫入資料和許可權,因此檔案的建立時間、修改時間是新的
若路徑下test1.txt存在,則會覆蓋其中的資料,寫入新資料和許可權,因此檔案的修改時間是新的
原始碼:
def copy(src, dst, *, follow_symlinks=True):
"""Copy data and mode bits ("cp src dst"). Return the file's destination.
The destination may be a directory.
If follow_symlinks is false, symlinks won't be followed. This
resembles GNU's "cp -P src dst".
If source and destination are the same file, a SameFileError will be
raised.
"""
if os.path.isdir(dst):
dst = os.path.join(dst, os.path.basename(src))
copyfile(src, dst, follow_symlinks=follow_symlinks)
copymode(src, dst, follow_symlinks=follow_symlinks)
return dst
3. 複製檔案內容和狀態
shutil.copy2(oldpath, newpath)
oldpath是原檔案路徑,newpath可以是新檔案路徑,也可以是資料夾路徑,示例:
import shutil
oldpath = 'G:\\test1\\test1.txt'
newpath1 = 'G:\\test2\\test2.txt'
newpath2 = 'G:\\test2'
shutil.copy2(oldpath, newpath1)
shutil.copy2(oldpath, newpath2)
解釋:
(1)newpath是新檔案路徑時:
若test2.txt不存在,會新建test2.txt後寫入資料和狀態,因此檔案的建立時間是新的,修改時間與test1.txt相同
若test2.txt存在,則會覆蓋其中的資料,寫入新資料和狀態,因此檔案的建立時間是新的,修改時間與test1.txt相同
(2)newpath是資料夾路徑時:
若路徑下test1.txt不存在,會新建test1.txt後寫入資料和狀態,因此檔案的建立時間是新的,修改時間與test1.txt相同
若路徑下test1.txt存在,則會覆蓋其中的資料,寫入新資料和狀態,因此檔案的建立時間是新的,修改時間與test1.txt相同
原始碼:
def copy2(src, dst, *, follow_symlinks=True):
"""Copy data and all stat info ("cp -p src dst"). Return the file's
destination."
The destination may be a directory.
If follow_symlinks is false, symlinks won't be followed. This
resembles GNU's "cp -P src dst".
"""
if os.path.isdir(dst):
dst = os.path.join(dst, os.path.basename(src))
copyfile(src, dst, follow_symlinks=follow_symlinks)
copystat(src, dst, follow_symlinks=follow_symlinks)
return dst
以上三個函式均不會新建資料夾,因此newpath中包含的資料夾路徑必須完整存在
推薦使用copy2複製函式,因為它會同時寫入資料和狀態(包括許可權,組,使用者,時間等)
二、複製資料夾
shutil.copytree(oldpath, newpath)
oldpath是待複製的資料夾路徑,newpath是複製後的新資料夾路徑,示例:
import shutil
oldpath = 'G:\\test\\test1'
newpath = 'G:\\test\\test2\\test3'
shutil.copytree(oldpath, newpath)
解釋:
(1)如果test3資料夾不存在,會生成一個名為test3的資料夾,其內容與狀態與test1相同
(2)如果test3資料夾存在,執行shutil.copytree()會報錯
移動
一、移動檔案
os.renames(oldpath, newpath)
oldpath是原檔案路徑,newpath是新檔案路徑,示例:
import os
oldpath = 'G:\\test1\\test1.txt'
newpath = 'G:\\test2\\test2.txt'
os.renames(oldpath, newpath)
解釋:
執行os.renames()會讓test1.txt移動至test2目錄下,同時重新命名為test2.txt
二、移動檔案或資料夾
1. 移動檔案
shutil.move(oldpath, newpath)
oldpath是原檔案路徑,newpath可以是檔案路徑,也可以是資料夾路徑,示例:
import shutil
oldpath1 = 'G:\\test1\\test1.txt'
newpath1 = 'G:\\test2\\test2.txt'
shutil.move(oldpath1, newpath1)
oldpath2 = 'G:\\test3\\test3.txt'
newpath2 = 'G:\\test4'
shutil.move(oldpath2, newpath2)
解釋:
(1)執行shutil.move()會讓test1.txt移動至test2目錄下,同時重新命名為test2.txt
(2)執行shutil.move()會讓test3.txt移動至test4目錄下
2. 移動資料夾
shutil.move(oldpath, newpath)
oldpath是原資料夾路徑,newpath是新資料夾路徑,示例:
import shutil
oldpath1 = 'G:\\test\\test1'
newpath1 = 'G:\\test\\test2'
shutil.move(oldpath1, newpath1)
解釋:
執行shutil.move()會讓test1目錄移動至test2目錄下,即在test2目錄下會存在一個名為test1的目錄
shutil.move()和shutil.copytree()不太一樣的地方就在於newpath的設定:
copytree的新資料夾路徑即是複製的資料夾路徑
move的新資料夾路徑是移動的資料夾父路徑