Esempio n. 1
0
    def copy(source_file,save_dir):
        """复制文件/文件夹
        """
        try:
            if not os.path.exists(save_dir):
                os.makedirs(save_dir)
            basename = os.path.basename(source_file)
            if os.path.isfile(source_file):
                file_name = os.path.splitext(basename)[0]
                file_ext = os.path.splitext(basename)[1]
                save_filepath = os.path.join(save_dir,basename)
                if os.path.exists(save_filepath):
                    save_filepath = os.path.join(save_dir,file_name + '_' + StringHelper.get_random_num() + file_ext)
                shutil.copy(source_file,save_filepath)
            elif os.path.isdir(source_file):
                save_dir_path = os.path.join(save_dir,basename)
                if os.path.exists(save_dir_path):
                    save_dir_path = os.path.join(save_dir,basename + '_' + StringHelper.get_random_num())
                shutil.copytree(source_file,save_dir_path)
            else:
                return False,source_file + ' 不是目录也不是文件'

            return True,''
        except Exception as e:
            return False,str(e)
Esempio n. 2
0
 def create_archive_bulk(files, archive_type):
     """创建多个存档
     返回 flag,msg   flag:操作是否成功 msg成功则为存档文件路径,失败则为错误信息
     """
     msg = ''
     flag = True
     save_file_name = StringHelper.get_random_num() + '多个文件'
     is_gztar = False
     if archive_type == 'gztar':  #gztar无法设置为追加 所以先打包为tar然后在打包为gztar
         is_gztar = True
         archive_type = 'tar'
     for file in files:
         _flag, _msg = FileHelper.create_archive(
             file, archive_type, save_file_name=save_file_name,
             mode='a')  #设置为追加
         flag = _flag and flag
         if _msg != '':
             if msg == '':
                 msg = _msg
             else:
                 msg = msg + ',' + _msg
     if is_gztar:
         tarfilepath = msg.split(',')[0]
         _flag, _msg = FileHelper.create_archive(tarfilepath, 'gztar')
         FileHelper.delete(tarfilepath)
         flag = _flag and flag
         msg = msg + ',' + _msg
     return flag, msg