python 如何復制整個文件夾到另一個目錄下連文件夾也要復制過去

在 liunx下:

>>>importos
>>>os.system("cp-rf/folder/home/user/folder")

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

用readline

inputFile = open("inputFile.txt", "r")
print "Name of the input file: ", inputFile.name;

outputFile = open("outputFile.txt", "a");
print "Name of the output file: ", outputFile.name;

allLines = inputFile.readlines();
for eachLine in allLines:
print "current line content: %s" % (eachLine);

#append into output file
outputFile.write(eachLine);

inputFile.close();
outputFile.close();

⑶ python 拷貝文件的問題。

就是建立目標目錄就可以
souredir=r'D:\1'
destdir=r'D:\2'

import glob,os
files=glob.glob(souredir)
for fn in files:
fn2=fn.replace(sourcedir,destdir)
subdir=os.path.dirname(fn2)
if not os.isdir(subdir): os.makedirs(subdir) #這里建立所有子目錄
open(fn2,"wb").write(open(fn1,"rb").read())
你檢查一下,這樣能滿足你的要求嗎?

⑷ python shutil模塊函數「file」和「」有什麼區別

file(src, dst) #src, dst 都需是文件名, 如果dst 存在或無許可權,會拋出異常
(src, dst) #dst 可以是目錄名。

⑸ python怎麼實現文件的復制

給你看一段樣例代碼專屬

defFile(src,des):
srcFp=open(src,"r")
desFp=open(des,"w")
ch=srcFp.read(1)
whilech!="":
desFp.write(ch)
ch=srcFp.read(1)
srcFp.close()
desFp.close()
File("f:\new.txt","f:\test.txt")

⑹ python 中如何實現對文件的復制、粘貼

用shutil模塊
#!/usr/bin/env python
#-*- coding: utf-8 -*-

import os
import os.path
from shutil import

dest_dir = ur'd:\新建文件夾'
if not os.path.isdir(dest_dir):
os.makedirs(dest_dir)

file_path = ur'c:\123\1.txt'
(file_path, dest_dir)

⑺ 用python把文件夾下的所有文件包括文件夾裡面的文件都拷貝到同一個目錄下

importos
importshutil

defwenjian(path):
ifos.path.isfile(path):
shutil.(path,'c:\new_dir')
ifos.path.isdir(path):
lists=os.listdir(path)
foriinlists:
wenjian(i)

foriinos.walk('c:\1'):
wenjian(i)

建議你把檢索到的文件都放到一個新的文件夾里,要不然系統在內同一個文件夾里不停的讀取和容寫入可能會陷入死循環以至出錯。

⑻ python 復制文件

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

1、首先插入必要的庫:

importos
importos.path
importshutil
importtime,datetime

2、實現復制文件代碼如下:

defFiles(sourceDir,targetDir):
ifsourceDir.find(".svn")>0:
return
forfileinos.listdir(sourceDir):
sourceFile=os.path.join(sourceDir,file)
targetFile=os.path.join(targetDir,file)
ifos.path.isfile(sourceFile):
ifnotos.path.exists(targetDir):
os.makedirs(targetDir)
ifnotos.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())
ifos.path.isdir(sourceFile):
First_Directory=False
Files(sourceFile,targetFile)

3、主函數的實現:

if__name__=="__main__":
print"Start(S)orQuilt(Q) "
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
Files(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"allsucess"
else:
print"notthecorrectcommand"

⑼ python怎麼拷貝文件夾下的文件

def upload_file(src_path, dst_path):
# 目標目錄是否存在,不存在則創建
if not os.path.exists(os.path.dirname(dst_path)):
os.makedirs(os.path.dirname(dst_path))
# 本地文件是否存在,存在則移動到目標目錄下
if os.path.exists(src_path):
shutil.move(src_path, dst_path)

⑽ 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