① 用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())