import os.path ,shutil #shutil包系統自帶,無需安裝
#複製單個檔案
shutil.copy(
"c://a"
,"c://b"
)#複製並重命名新檔案
shutil.copyfile(
"c://a"
,"c://b"
)#複製整個目錄(備份)
shutil.copytree(
"c://a"
,"c://b//new_a"
)#刪除檔案
os.unlink(
"c://b"
)os.unlink(
"c://b"
)#刪除空資料夾
try:
os.rmdir(
"c://b//new_a"
)except exception as ex:
print
("錯誤資訊:"
+str
(ex)
)#刪除資料夾及內容
shutil.rmtree(
"c://b//new_a"
)#移動檔案
shutil.move(
"c://a"
,"c://b"
)#移動資料夾
shutil.move(
"c://a//c"
,"c://b"
)#重新命名檔案
shutil.move(
"c://a"
,"c://a"
)#重新命名資料夾
shutil.move(
"c://a//d"
,"c://a//new_d"
)
將多個pdf文件合併為乙個pdf文件,這裡將200多個快遞單(pdf檔案)合併為乙個pdf文件
import os
from pypdf2 import pdffilereader, pdffilewriter
import shutil
path=
input
("輸入路徑"
)out_path=
open
(r"c:\users\dq515rs\desktop\需列印的快遞單"
+os.sep+
"合併.pdf"
,"wb"
)os.chdir(path)
all_file=os.listdir(os.curdir)
f=pdffilewriter(
)#若把f放到迴圈裡邊會導致只合併最後乙個
for i in all_file:
pdf_open_i=
open
(path+os.sep+i,
"rb"
) read_pdf_i=pdffilereader(pdf_open_i)
n_i=read_pdf_i.getnumpages(
)for j in
range
(n_i)
: f.addpage(read_pdf_i.getpage(j)
)f.write(out_path)
out_path.close(
)#不關閉文件會出錯
移動資料夾
問題 將資料夾及其下面的檔案移動到其他目錄 1 using quality.tools.common.log 2using system 3using system.collections.generic 4using system.configuration 5using system.io 6u...
python 移動檔案或資料夾操作
無意中發現了乙個巨牛的人工智慧教程,忍不住分享一下給大家。教程不僅是零基礎,通俗易懂,而且非常風趣幽默,像看 一樣!覺得太牛了,所以分享給大家。點這裡可以跳轉到教程。1.得到當前工作目錄,即當前python指令碼工作的目錄路徑 os.getcwd 2.返回指定目錄下的所有檔案和目錄名 os.list...
ftp 移動資料夾
已經可以移動檔案了,原因是路徑問題。還是用的rename方法。原因是renameto 這裡的路徑之前沒包含檔名,而且相對路徑和絕對路徑都沒弄對,所以之前一直不相信別人說的rename可以移動檔案。現在結貼了,謝謝大家!為了讓後來人能夠找到答案,我把 貼出來,做個示範 ftp 10.62.80.x t...