導航:首頁 > 編程語言 > python復制文件到指定位置覆蓋

python復制文件到指定位置覆蓋

發布時間:2022-11-03 07:12:47

① 用python如何將文件夾內部分指定文件名的文件復制到目標文件夾,大佬求教!

import glob
import shutil

def _file(names,old_name,new_name):
for name in names:
filename = name.split("\\")[-1]
#filename:從路徑中截取文件名
shutil.file(old_name + filename, new_name + filename)

files = glob.glob(r'D:/A/1*.txt')
#files : 搜索得到的符合條件(帶有1開頭的txt)的文件列表
old_path = r'D:/A/'
new_path = r'D:/B/'
_file(files,old_path,new_path)

② python 實現一級目錄下的所有文件與文件夾到指定目錄

'''
python3 實現
將a目錄下所有文件和文件夾到b目錄
'''
import os, shutil

#src 原始目錄, des 目標目錄
def sourcecpy(src, des):
src = os.path.normpath(src)
des = os.path.normpath(des)
if not os.path.exists(src) or not os.path.exists(src):
print("文件路徑不存在")
sys.exit(1)
#獲得原始目錄中所有的文件,並拼接每個文件的絕對路徑
os.chdir(src)
src_file = [os.path.join(src, file) for file in os.listdir()]
for source in src_file:
#若是文件
if os.path.isfile(source):
shutil.(source, des) #第一個參數是文件,第二個參數目錄
#若是目錄
if os.path.isdir(source):
p, src_name = os.path.split(source)
des = os.path.join(des, src_name)
shutil.tree(source, des) #第一個參數是目錄,第二個參數也是目錄

③ Python如何將指定文件夾(包括裡面的內容)到指定目錄(已存在)

使用這個方法

importshutil,errno

defanything(src,dst):
try:
shutil.tree(src,dst)
exceptOSErrorasexc:#python>2.5
ifexc.errno==errno.ENOTDIR:
shutil.(src,dst)
else:raise

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

使用了os和shutil兩個模塊,os.listdir用於讀取目標目錄中的文件名稱,star文件夾中存儲了我手動篩選出來的DNG格式圖片,jpg文件夾中存儲了所有的jpg格式圖片,於是在獲取到所有DNG格式圖片後使用flag數組標記一下
然後循環遍歷targetnames數組尋找對應名稱的jpg文件,找到的話就使用shutil.file復制到指定文件夾,然後就大功告成了!

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

使用os和shutil兩個模塊,os.listdir用於讀取目標目錄中的文件名稱,star文件夾中存儲了我手動篩選出來的DNG格式圖片,jpg文件夾中存儲了所有的jpg格式圖片,於是在獲取到所有DNG格式圖片後使用flag數組標記一下
然後循環遍歷targetnames數組尋找對應名稱的jpg文件,找到的話就使用shutil.file復制到指定文件夾,然後就大功告成了!

⑥ 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模塊是另一個文件,目錄的管理介面,提供了一些用於復制文件,目錄的函數。file()函數可以實現文件的拷貝,聲明如下:
file(src, des)
文件的剪切可以使用move()函數模擬,聲明如下:
move(src,des)
功能:移動一個文件或者目錄到指定的位置,並且可以根據參數des重命名移動後的文件。

⑦ 如何用python復制粘貼文件到指定文件夾,windows

熟練掌握App發布的流程,真機調試技巧,證書,打包,上架。
App開發其實不一定適合一個人搞,太費勁,除非是一個單機版的小應用,或者利用現成的app開發簡單的第三方應用,否則還是讓一個團隊來完成各自擅長的領域。

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

代碼:

import os
import shutil
from shutil import Error
from shutil import stat
from shutil import 2
src = "" #需要復制的文件目錄
dst = "" #目標目錄
def jiecptree(src, dst, symlinks=False, ignore=None): #聲明函數 ree( 要復制的目錄,目標目錄,復制符號連接內容到新目錄,沒有要忽略文件)
names = os.listdir(src) #獲得要復制目錄的文件名列表,賦給變數 names
if ignore is not None: #如果 ignore 不是None值
ignored_names = ignore(src, names) # src目錄中要忽略文件的名字賦給 ignored_names
else: # 否則
ignored_names = set() #ignore_name 被 不重復空元素集 賦值
if os.path.isdir(dst):
pass
else:
os.makedirs(dst)
# print"dstfirst:"+dst
errors = [] #聲明 errors列
for name in names: #將names里的元素循環復制給name
if name in ignored_names: #如果name在要求被忽略的列里出現
continue #繼續for循環(跳回for,從新循環下個元素)
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)
2(srcname, dstname)
else: # 否則
2(srcname, dstname) # 復制srcname到dstname
# print "srcname:"+srcname
# print "dstname:"+dstname
# XXX What about devices, sockets etc.? #怎樣裝置
except (IOError, os.error), why: #除(IOError[與文件有關的異常],操作系統異常)外,返回原因
errors.append((srcname, dstname, str(why))) # 向errors列里添加,(要復制的目錄,目標目錄,錯誤原因)
# catch the Error from the recursive jiecptree so that we can 從遞歸復制中捕捉這個錯誤,以便於我們能繼續復制其他文件
# continue with other files
except Error, err: #除錯誤外,返回錯誤:
errors.extend(err.args[0]) #擴展 errors 列,添加(err.args[0] 元素)
try: #嘗試
stat(src, dst) # 從src復制許可權位,上次訪問時間,最後修改時間 到 dst,
except WindowsError: # 除 Windows錯誤 外:
# can't file access times on Windows 在Windows上無法復制文件訪問時間
pass # 通過(不作任何處理)
except OSError, why: # 除 操作系統錯誤 外,返回原因:
errors.extend((src, dst, str(why))) #擴展 errors 列,添加(要復制的目錄,目標目錄,錯誤原因)
if errors: # 如果錯誤
raise Error(errors) # 提示錯誤

更多相關內容可參考資料http://www.viiboo.cn

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

好像不能直接import os搞定,得import shutil

importshutil
#復制文件
shutil.file('listfile.py','d:/test.py')
#復制目錄
shutil.tree('d:/temp','c:/temp/')
#其餘可以參考shutil下的函數

⑩ python 復制文件

報錯多半是這句targetDir = targetDir+'/'+os.path.split(sourceDir)[1]

你這句把本來的targetDir覆蓋了,導致後面的文件的目標文件夾被修改


發個我寫的吧,參考下吧

defFile(sourceDir,targetDir):
ifnotos.path.exists(targetDir):
os.makedirs(targetDir)

forfilenameinos.listdir(sourceDir):
path=os.path.join(sourceDir,filename)
ifos.path.isdir(path):
targetSubDir=os.path.join(targetDir,filename)
File(path,targetSubDir)
else:
targetPath=os.path.join(targetDir,filename)
open(targetPath,'wb').write(open(path,'rb').read())
閱讀全文

與python復制文件到指定位置覆蓋相關的資料

熱點內容
自動解壓失敗叫我聯系客服 瀏覽:482
易語言新手源碼 瀏覽:456
oa伺服器必須有固定ip地址 瀏覽:42
傳奇源碼分析是什麼 瀏覽:267
解放壓縮機支架 瀏覽:255
程序員禿頂搞笑相遇 瀏覽:6
IBM手機app商店叫什麼名字 瀏覽:834
jpeg壓縮質量 瀏覽:774
雲伺服器評測對比 瀏覽:145
java日期轉string 瀏覽:221
openfire源碼編譯 瀏覽:897
在線小工具箱引流網站源碼 瀏覽:337
非科班程序員自學 瀏覽:800
壓縮泡沫鞋底底材 瀏覽:219
程序員職場第一課2正確的溝通 瀏覽:679
遇到不合法app應該怎麼辦 瀏覽:91
匯編程序編譯後的文件 瀏覽:80
大智慧均線源碼 瀏覽:374
單片機排阻的作用 瀏覽:216
滴滴金融app被下架如何還款 瀏覽:212