文件復(fù)制函數(shù)python,復(fù)制函數(shù)內(nèi)容

python IDEL復(fù)制上一行的快捷鍵是什么

1、新建一個python腳本文件file.py(名字任意)。

員工經(jīng)過長期磨合與沉淀,具備了協(xié)作精神,得以通過團隊的力量開發(fā)出優(yōu)質(zhì)的產(chǎn)品。創(chuàng)新互聯(lián)堅持“專注、創(chuàng)新、易用”的產(chǎn)品理念,因為“專注所以專業(yè)、創(chuàng)新互聯(lián)網(wǎng)站所以易用所以簡單”。公司專注于為企業(yè)提供成都做網(wǎng)站、成都網(wǎng)站建設(shè)、微信公眾號開發(fā)、電商網(wǎng)站開發(fā),小程序設(shè)計,軟件按需規(guī)劃網(wǎng)站等一站式互聯(lián)網(wǎng)企業(yè)服務(wù)。

2、在python腳本中用2個open函數(shù)即可實現(xiàn)對該圖片的復(fù)制,具體實現(xiàn)代碼如圖。

3、執(zhí)行上述腳本文件后查看結(jié)果,可以看到該圖片已被成功復(fù)制。

4、對于其他文件例如,文本文件操作方式和上面一樣,下圖是復(fù)制11.txt成功的實際例子。以上文件操作都需要注意文件的路徑,路徑可以是絕對路徑類似"F:\test418\examples\11.txt",也可以是相對路徑類似"11.txt"

python中怎樣將文件拷貝到指定的目錄下

代碼:

import os

import shutil

from shutil import Error

from shutil import copystat

from shutil import copy2

src = "" #需要復(fù)制的文件目錄

dst = "" #目標(biāo)目錄

def jiecptree(src, dst, symlinks=False, ignore=None): #聲明函數(shù) copyree( 要復(fù)制的目錄,目標(biāo)目錄,復(fù)制符號連接內(nèi)容到新目錄,沒有要忽略文件)

names = os.listdir(src) #獲得要復(fù)制目錄的文件名列表,賦給變量 names

if ignore is not None: #如果 ignore 不是None值

ignored_names = ignore(src, names) # src目錄中要忽略文件的名字賦給 ignored_names

else: # 否則

ignored_names = set() #ignore_name 被 不重復(fù)空元素集 賦值

if os.path.isdir(dst):

pass

else:

os.makedirs(dst)

# print"dstfirst:"+dst

errors = [] #聲明 errors列

for name in names: #將names里的元素循環(huán)復(fù)制給name

if name in ignored_names: #如果name在要求被忽略的列里出現(xiàn)

continue #繼續(xù)for循環(huán)(跳回for,從新循環(huán)下個元素)

srcname = os.path.join(src, name) #將路徑名(src)添加到文名(name)之前然后賦值給 srcname

dstname = os.path.join(dst, name) #將路徑名(dst)添加到文名(name)之前然后賦值給 dstcname

from shutil import Error

# print "name:"+name

# print "src:"+src

# print "dst:"+dst

try: #嘗試

if os.path.islink(srcname):

continue

elif os.path.isdir(srcname): #如果srcname路徑是存在

jiecptree(srcname, dstname, symlinks, ignore)

elif os.path.isdir(dstname):

os.remove(dstname)

copy2(srcname, dstname)

else: # 否則

copy2(srcname, dstname) # 復(fù)制srcname到dstname

# print "srcname:"+srcname

# print "dstname:"+dstname

# XXX What about devices, sockets etc.? #怎樣裝置

except (IOError, os.error), why: #除(IOError[與文件有關(guān)的異常],操作系統(tǒng)異常)外,返回原因

errors.append((srcname, dstname, str(why))) # 向errors列里添加,(要復(fù)制的目錄,目標(biāo)目錄,錯誤原因)

# catch the Error from the recursive jiecptree so that we can 從遞歸復(fù)制中捕捉這個錯誤,以便于我們能繼續(xù)復(fù)制其他文件

# continue with other files

except Error, err: #除錯誤外,返回錯誤:

errors.extend(err.args[0]) #擴展 errors 列,添加(err.args[0] 元素)

try: #嘗試

copystat(src, dst) # 從src復(fù)制權(quán)限位,上次訪問時間,最后修改時間 到 dst,

except WindowsError: # 除 Windows錯誤 外:

# can't copy file access times on Windows 在Windows上無法復(fù)制文件訪問時間

pass # 通過(不作任何處理)

except OSError, why: # 除 操作系統(tǒng)錯誤 外,返回原因:

errors.extend((src, dst, str(why))) #擴展 errors 列,添加(要復(fù)制的目錄,目標(biāo)目錄,錯誤原因)

if errors: # 如果錯誤

raise Error(errors) # 提示錯誤

更多相關(guān)內(nèi)容可參考資料

python 復(fù)制文件

用Python把某一目錄下的文件復(fù)制到指定目錄中,代碼如下:

1、首先插入必要的庫:

import?os?

import?os.path?

import?shutil?

import?time,??datetime

2、實現(xiàn)復(fù)制文件代碼如下:

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、主函數(shù)的實現(xiàn):

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的shutil模塊中文件的復(fù)制操作

shutil.copyfile(src, dst):將名為src的文件的內(nèi)容復(fù)制到名為dst的文件中 。

src, dst是文件名

shutil.copy(source, destination)

shutil.copy() 函數(shù)實現(xiàn)文件復(fù)制功能,將 source 文件復(fù)制到 destination 文件夾中,兩個參數(shù)都是字符串格式。如果 destination 是一個文件名稱,那么它會被用來當(dāng)作復(fù)制后的文件名稱,即等于 復(fù)制 + 重命名。

source一定是文件名,destination可以是文件名也可以是文件夾名

舉例如下:

shutil 模塊

python 中如何實現(xiàn)對文件的復(fù)制、粘貼

file類中沒有提供專門的文件復(fù)制函數(shù),因此只能通過使用文件的讀寫函數(shù)來實現(xiàn)文件的復(fù)制。這里僅僅給出范例:

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模塊是另一個文件,目錄的管理接口,提供了一些用于復(fù)制文件,目錄的函數(shù)。copyfile()函數(shù)可以實現(xiàn)文件的拷貝,聲明如下:

copyfile(src, des)

文件的剪切可以使用move()函數(shù)模擬,聲明如下:

move(src,des)

功能:移動一個文件或者目錄到指定的位置,并且可以根據(jù)參數(shù)des重命名移動后的文件。

新聞名稱:文件復(fù)制函數(shù)python,復(fù)制函數(shù)內(nèi)容
本文鏈接:http://muchs.cn/article44/hcpiee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作Google、電子商務(wù)、營銷型網(wǎng)站建設(shè)動態(tài)網(wǎng)站、域名注冊

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計