python文件拷贝
⑴ 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