本文目錄一覽:
- 1、python 中如何實現對文件的複製、粘貼
- 2、python 怎麼把文件夾下所有文件複製
- 3、說說在 Python 中如何複製、移動、改名以及刪除文件或文件夾
- 4、python 複製文件
- 5、用python提取csv文件內容到數據庫
python 中如何實現對文件的複製、粘貼
file類中沒有提供專門的文件複製函數,因此只能通過使用文件的讀寫函數來實現文件的複製。這裡僅僅給出範例:
src = file(“myfile.txt”, “w+”)
temp = [“hello world! \n”]
src.writelines(temp)
src.close()
src = file(“myfile.txt”, “r+”)
des = file(“myfile2.txt”, “w+”)
des.writelines(src.read())
src.close()
des.close()
shutil模塊是另一個文件,目錄的管理接口,提供了一些用於複製文件,目錄的函數。copyfile()函數可以實現文件的拷貝,聲明如下:
copyfile(src, des)
文件的剪切可以使用move()函數模擬,聲明如下:
move(src,des)
功能:移動一個文件或者目錄到指定的位置,並且可以根據參數des重命名移動後的文件。
python 怎麼把文件夾下所有文件複製
import
os
import
os.path
rootdir
=
「d:\data」
#
指明被遍歷的文件夾
for
parent,dirnames,filenames
in
os.walk(rootdir):
#三個參數:分別返回1.父目錄
2.所有文件夾名字(不含路徑)
3.所有文件名字
for
dirname
in
dirnames:
#輸出文件夾信息
“parent
is:”
+
parent
“dirname
is:”
+
dirname
for
filename
in
filenames:
#輸出文件信息
“parent
is:”
+
parent
“filename
is:”
+
filename
“the
full
name
of
the
file
is:”
+
os.path.join(parent,filename)
#輸出文件路徑信息
說說在 Python 中如何複製、移動、改名以及刪除文件或文件夾
要實現複製、移動、改名以及刪除文件或文件夾,需要用到 shutil 模塊,shutil 是 shell util 的簡寫形式,表示 shell 工具。
調用 shutil.copy(source, destination) 來實現複製文件或文件夾功能,依據 destination 進行區分:
運行結果:
注意: 指定複製的文件夾必須存在,否則會拋出 FileNotFoundError。
shutil 的 copytree(source, destination) 方法會複製整個文件夾,包括它所包含的所有文件夾和文件。source
指定源文件夾,destination 指定新的文件夾。source 和 destination 入參都是字符串。該函數會返回新文件夾的路徑。destination 如果不存在,會自動創建。請看下例:
運行結果:
shutil.move(source, destination) 方法會將路徑 source 處的文件移動到路徑 destination,並返回新位置的絕對路徑的字符串。
如果 destination 指向一個文件夾, source 處的文件將移動到 destination 中, 並保持原來的文件名。
運行結果:
注意:
os 模塊中的函數,可以實現刪除一個文件或一個空文件夾。而 shutil 更強大,使用它可以刪除一個非空文件夾!
注意: 因為是永久刪除,所以使用這些函數一定要小心!建議調試程序時, 先注釋掉這些刪除方法,
然後加上 print(), 把要被刪除的文件打印出來,確認後,再執行。
打印出來的文件列表確認無誤後,再執行 os.unlink(filename) 執行刪除操作。
send2trash 模塊會將文件夾或文件發送到計算機的回收站。首先,安裝它:
安裝成功後,調用 send2trash.send2trash 方法,就可以把文件夾或文件發送到計算機的回收站。請看下例:
建議使用 send2trash.send2trash() 函數來刪除文件或文件夾,因為以後還可以從回收站還原。但這樣做,不
會釋放磁盤空間。如果我們還是希望程序釋放磁盤空間, 就要用 os 和 shutil 來刪除文件和
文件夾(記得使用之前提出的 print 技巧)。還有一點需要注意, send2trash() 函數只能將文件送到回收站, 但不能從回收站中恢復文件。
python 複製文件
用Python把某一目錄下的文件複製到指定目錄中,代碼如下:
1、首先插入必要的庫:
import os
import os.path
import shutil
import time, datetime
2、實現複製文件代碼如下:
def copyFiles(sourceDir,targetDir):
if sourceDir.find(“.svn”) 0:
return
for file in os.listdir(sourceDir):
sourceFile = os.path.join(sourceDir,file)
targetFile = os.path.join(targetDir,file)
if os.path.isfile(sourceFile):
if not os.path.exists(targetDir):
os.makedirs(targetDir)
if not os.path.exists(targetFile) or(os.path.exists(targetFile) and (os.path.getsize(targetFile) != os.path.getsize(sourceFile))):
open(targetFile,”wb”).write(open(sourceFile,”rb”).read())
if os.path.isdir(sourceFile):
First_Directory = False
copyFiles(sourceFile, targetFile)
3、主函數的實現:
if __name__ ==”__main__”:
print “Start(S) or Quilt(Q) \n”
flag = True
while (flag):
answer = raw_input()
if ‘Q’ == answer:
flag = False
elif ‘S’== answer :
formatTime = getCurTime()
targetFoldername = “Build ” + formatTime + “-01”
Target_File_Path += targetFoldername
copyFiles(Debug_File_Path,Target_File_Path)
removeFileInFirstDir(Target_File_Path)
coverFiles(Release_File_Path,Target_File_Path)
moveFileto(Firebird_File_Path,Target_File_Path)
moveFileto(AssistantGui_File_Path,Target_File_Path)
writeVersionInfo(Target_File_Path+”\\ReadMe.txt”)
print “all sucess”
else:
print “not the correct command”
用python提取csv文件內容到數據庫
這個腳本可以直接運行,將csv文件放在同級目錄即可。
csv第一列需要有列名,如果csv里沒有列名,需要在代碼中添加列名。
代碼運行示例:python insert.py csvname tablename
原創文章,作者:XERU,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/144373.html