一、shutil -- 是一種高層次的文件操作工具類似於高級API,而且主要強大之處在於其對文件的復制與刪除操作更是比較支持好。
1、shutil.copy(src,dst)復制一個文件到另一個目錄下,返回dst路徑。dst可以是一個文件,或者是一個目錄。但src必須是一個文件,否則會報錯。
>>> shutil.copy("e:\\test\\pp.txt","f:\\yy.txt") 'f:\\yy.txt' >>> shutil.copy("e:\\test","f:\\") Traceback (most recent call last): File "<pyshell#15>", line 1, in <module> shutil.copy("e:\\test","f:\\") File "D:\Program Files\python3.6\lib\shutil.py", line 235, in copy copyfile(src, dst, follow_symlinks=follow_symlinks) File "D:\Program Files\python3.6\lib\shutil.py", line 114, in copyfile with open(src, 'rb') as fsrc: PermissionError: [Errno 13] Permission denied: 'e:\\test'
2、shutil.copy2(src,dst)在copy上的基礎上再復制文件最后訪問時間與修改時間也復制過來了
3、shutil.copyfile(src,dst)從源src復制到dst中去,src和dst必須是文件,不能是文件夾
>>> shutil.copyfile("e:\\test\\pp.txt","f:\\oo.txt") 'f:\\oo.txt' >>> shutil.copyfile("e:\\test\\pp.txt","f:\\") Traceback (most recent call last): File "<pyshell#19>", line 1, in <module> shutil.copyfile("e:\\test\\pp.txt","f:\\") File "D:\Program Files\python3.6\lib\shutil.py", line 115, in copyfile with open(dst, 'wb') as fdst: FileNotFoundError: [Errno 2] No such file or directory: 'f:\\'
4、shutil.copystat( src, dst)只復制權限、最后訪問時間、最后修改時間,不會復制文件內容
5、shutil.copytree(olddir,newdir,True/Flase)把olddir拷貝一份newdir,如果第3個參數是True,則復制目錄時將保持文件夾下的符號連接,如果第3個參數是False,則將在復制的目錄下生成物理副本來替代符號連接。
>>> shutil.copytree("e:\\test\\","f:\\yy") 'f:\\yy'
6、shutil.copymode(src,dst)只復制權限
7、shutil.move(src,dst)將路徑src處的文件夾移動到路徑dst,並返回新位置的絕對路徑字符串。src可以是一個文件夾,也可以是一個文件。
如果dst目錄下已經存在了src中的文件名,則會報錯。
>>> shutil.move("e:\\test\\pp.txt","f:\\yy\\uu") 'f:\\yy\\uu' >>> shutil.move("e:\\test","f:\\") 'f:\\test' >>> shutil.move("e:\\answer1.txt","f:\\yy") Traceback (most recent call last): File "<pyshell#27>", line 1, in <module> shutil.move("e:\\answer1.txt","f:\\yy") File "D:\Program Files\python3.6\lib\shutil.py", line 536, in move raise Error("Destination path '%s' already exists" % real_dst) shutil.Error: Destination path 'f:\yy\answer1.txt' already exists
這里注意一點:如果src中的文件只有只讀權限,則會報錯,但src文件仍然正常的copy到了dst下。但src不會被刪除
>>> shutil.move("e:\\answer1.txt","f:\\yy") Traceback (most recent call last): File "D:\Program Files\python3.6\lib\shutil.py", line 538, in move os.rename(src, real_dst) OSError: [WinError 17] 系統無法將文件移到不同的磁盤驅動器。: 'e:\\answer1.txt' -> 'f:\\yy\\answer1.txt' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "<pyshell#28>", line 1, in <module> shutil.move("e:\\answer1.txt","f:\\yy") File "D:\Program Files\python3.6\lib\shutil.py", line 553, in move os.unlink(src) PermissionError: [WinError 5] 拒絕訪問。: 'e:\\answer1.txt'
如果dst所指的目錄不存在,而src是一個文件,所以程序默認會認為dst是指的一個沒有擴展名的文件,而不是一個文件夾。
>>> shutil.move("e:\\answer2.txt","f:\\qq") 'f:\\qq'
這樣就在f盤下生成了一個名為qq的文件,,如圖所示:
二、os和shutil模塊刪除文件和文件夾
1、os.unlink(path)將刪除path處的文件,成功刪除后沒有任何返回。path是一個文件的完整路徑
os.unlink("f:\\yy\\answer1.txt")
2、os.rmdir(path)將刪除path處的文件夾,該文件夾必須為空。其中沒有任何文件和文件夾
>>> os.rmdir("f:\\yy\\") Traceback (most recent call last): File "<pyshell#39>", line 1, in <module> os.rmdir("f:\\yy\\") OSError: [WinError 145] 目錄不是空的。: 'f:\\yy\\' >>> os.rmdir("f:\\yy\\test")
3、shutil.rmtree(path)將刪除path處的文件夾,它包含的所有文件和文件夾都將被刪除