当前位置: 首页 > news >正文

设计师交流网站电子商务网站建设与维护

设计师交流网站,电子商务网站建设与维护,济南卓远网站建设,成都装修公司招聘需要考虑的问题: 指定文件夹是否存在,不存在则创建在指定文件夹中是否存在同名文件,是覆盖还是另存为 import os import shutil import tracebackdef copyfile(srcfile, dstpath, replaceFalse):"""复制文件到指定文件夹par…

需要考虑的问题:

  • 指定文件夹是否存在,不存在则创建
  • 在指定文件夹中是否存在同名文件,是覆盖还是另存为
import os
import shutil
import tracebackdef copyfile(srcfile, dstpath, replace=False):"""复制文件到指定文件夹@param srcfile: 原文件绝对路径@param dstpath: 目标文件夹@param replace: 如果目标文件夹已存在同名文件,是否覆盖"""try:if not os.path.isfile(srcfile):print("%s not exist!" % (srcfile))else:fpath, fname = os.path.split(srcfile)  # 分离文件名和路径suffix = os.path.splitext(srcfile)[-1]# print(fpath, fname, suffix)if not os.path.exists(dstpath):os.makedirs(dstpath)  # 创建路径if replace:dstfile = os.path.join(dstpath, fname)shutil.copy(srcfile, dstfile)  # 复制文件print("copy %s -> %s" % (srcfile, dstfile))else:i = 1while True:add = ' (%s)' % str(i) if i != 1 else ''dstfile = os.path.join(dstpath, fname.replace(suffix, add + suffix))if os.path.exists(dstfile) and i <= 10:i += 1else:shutil.copy(srcfile, dstfile)  # 复制文件print("copy %s -> %s" % (srcfile, dstfile))breakreturn dstfileexcept Exception as e:print('文件复制失败', srcfile)traceback.print_exc()
import os
import shutil
import tracebackdef movefile(srcfile, dstpath, replace=False):"""移动文件到指定文件夹@param srcfile: 原文件绝对路径@param dstpath: 目标文件夹@param replace: 如果目标文件夹已存在同名文件,是否覆盖"""try:if not os.path.isfile(srcfile):print("%s not exist!" % (srcfile))else:fpath, fname = os.path.split(srcfile)  # 分离文件名和路径suffix = os.path.splitext(srcfile)[-1]# print(fpath, fname, suffix)if not os.path.exists(dstpath):os.makedirs(dstpath)  # 创建路径if replace:dstfile = os.path.join(dstpath, fname)shutil.move(srcfile, dstfile)  # 复制文件print("move %s -> %s" % (srcfile, dstfile))else:i = 1while True:add = ' (%s)' % str(i) if i != 1 else ''dstfile = os.path.join(dstpath, fname.replace(suffix, add + suffix))if os.path.exists(dstfile) and i <= 10:i += 1else:shutil.move(srcfile, dstfile)  # 复制文件print("move %s -> %s" % (srcfile, dstfile))breakexcept Exception as e:print('文件移动失败', srcfile)traceback.print_exc()

复制文件到指定文件夹V2:

  1. 判断源文件是否存在
  2. 判断目标文件夹是否存在
  3. 判断是否已存在该文件
  4. 判断已存在文件是否打开
  5. 判断是否需要替换掉已存在文件

注意:复制文件会改变时间属性(创建日期、修改日期),不再是源文件的时间属性

import os
import shutil
import tracebackdef copyfile(srcfile, dstpath, replace=False):"""复制文件到指定文件夹@param srcfile: 原文件绝对路径@param dstpath: 目标文件夹@param replace: 如果目标文件夹已存在同名文件,是否覆盖"""try:# 判断源文件是否存在assert os.path.isfile(srcfile), "源文件不存在"basename = os.path.basename(srcfile)fname = os.path.splitext(basename)[0]  # 不带后缀的文件名suffix = os.path.splitext(srcfile)[-1]# 判断目标文件夹是否存在if not os.path.exists(dstpath):os.makedirs(dstpath)  # 创建文件夹,可递归创建文件夹,可能创建失败# 判断目标文件夹是否存在assert os.path.exists(dstpath), "目标文件夹不存在"# 开始尝试复制文件到目标文件夹i = 0while True:i += 1add = '(%s)' % str(i) if i != 1 else ''dstfile = os.path.join(dstpath, fname + add + suffix)opened_dstfile = os.path.join(dstpath, '~$' + fname + add + suffix)  # 已打开文件# 判断目标文件夹是否存在该文件if not os.path.exists(dstfile):shutil.copy(srcfile, dstfile)  # 不存在则复制文件break# 存在该文件,则判断已存在文件是否打开if os.path.exists(opened_dstfile):# 已打开则创建下一个新文件continue# 已存在文件没有打开的情况if replace:shutil.copy(srcfile, dstfile)  # 复制文件break# 不覆盖已存在文件,则创建下一个新文件return dstfileexcept AssertionError as e:print('文件复制失败', e, srcfile)except Exception as e:print('文件复制失败', e, srcfile)if __name__ == "__main__":srcfile = r"C:\Users\Administrator\Desktop\源文件夹\test.txt"dir = r"C:\Users\Administrator\Desktop\目标文件夹"print(copyfile(srcfile, dir, replace=True))

Python复制文件到指定文件夹,遇到相同文件名的处理

https://www.cnblogs.com/johnthegreat/p/12748790.html

python复制、移动文件到指定文件夹_python移动文件到指定文件夹-CSDN博客

文件侠告诉你,Python复制文件的N种姿势! - 云+社区 - 腾讯云

http://www.dinnco.com/news/35452.html

相关文章:

  • 兄弟网络(西安网站建设制作公司)短视频营销
  • 模板网站可以自己买空间吗吗seoheuni
  • 自己建网站卖鞋青岛谷歌推广
  • 上海做网站的公司官网技能培训网
  • 网站301在哪里做青岛seo建站
  • 网站开发 方案 报价单培训机构营业执照如何办理
  • 合肥网站建设公司哪家好推广app赚钱的平台
  • 网站制作做网站培训推广 seo
  • 可以做哪些网站友情链接交换
  • 黑河网站建设查权重
  • 通辽做家教的网站标题优化seo
  • 枣庄手机网站建设电话百度推广找谁做
  • 江苏省工程造价信息网镇江seo优化
  • 云鼎大数据888元建站网络营销策划是什么
  • 东莞三合一网站制作郑州网站建设推广
  • 0网站建设的好坏可以依据的标准有宁波网站建设优化企业
  • 会网站建设好吗网络营销的优势有哪些?
  • 深圳前十网站建设公司日本域名注册网站
  • 做app网站设计seo排名优化推广
  • 怎么做网站门户写一篇软文1000字
  • 上海企业网站建设价格网络推广有哪些常见的推广方法
  • 龙岗网站-建设深圳信科潍坊网站建设解决方案
  • 只有一个页面的网站怎么做安徽seo顾问服务
  • 织梦网站采集侠怎么做考研培训
  • 网站app定制促销策略的四种方式
  • wordpress是不是一定要买服务器网站seo优化8888
  • 软件开发需要哪些人员杭州seo优化
  • 做网站如何月入过万百度人气榜
  • 经营网站如何挣钱重庆百度总代理
  • 市政府投资建设项目管理中心网站如何注册域名