导航:首页 > 编程语言 > python备份文件夹

python备份文件夹

发布时间:2023-08-22 00:16:06

python 菜鸟求助,在线等

备份一个文件,使用shutil库的函数可以达到这个目的:

⑵ python 保存csv到新的文件夹

你可以这样做:
import os
#获取当前文件夹,也就是当前py文件所处的文件夹
current_path = os.getcwd()

#结合文件夹
path = current_path+"\\data\\file1\\file2\\"
csvfile = file(path+"twse_com_tw.zho_CHN.UTF8.csv", 'wb')

⑶ 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备份文件夹相关的资料

热点内容
linux视频转换 浏览:692
linux与unixshell编程指南 浏览:129
vb6反编译程序错误 浏览:117
服务器弄挂有什么影响 浏览:298
安卓软件为什么运行不了 浏览:521
什么安卓手机是四方的 浏览:475
云服务器厂商租用多少钱 浏览:349
平安车贷解压要多久 浏览:412
乐一php程序员待遇 浏览:162
ZDM命令无法识别 浏览:919
解压小手工A4纸 浏览:981
钢筋加密区是几倍 浏览:669
编译程序代码软件 浏览:806
怎么恢复加密的东西 浏览:987
程序员卖茶 浏览:697
后端程序员英文 浏览:363
滴滴程序员平均月薪 浏览:591
如何使用ftp命令 浏览:788
小书亭下载的文件在哪手机文件夹 浏览:180
交叉编译器编译单个c文件 浏览:518