def upload_file(file, upload_dir='', filename='', max_upload_size=1024 * 1024, chmods='444'): ''' 用于web页面上传文件 :parm file:上传文件内容 upload_file:上传目录,默认:在项目根目录下的/file/upload filename:上传后的文件名,默认:10个随机字符+_+文件名 max_upload_size:最大上传文件大小,默认为1M :return False:上传失败 filename:上传成功后的文件名 ''' try: if file.size > max_upload_size: return (False, '上传文件超过最大值') if not filename: filename = random_str(ranlen=10) + '_' + file.name except Exception as e: return (False, '上传失败,可能是参数file出问题,' + str(e)) if upload_dir: result = path_isexists(upload_dir) if not result[0]: upload_dir = os.path.join(BASE_DIR, 'file/upload/') make_dir(upload_dir, force=False, backup=False) else: upload_dir = os.path.join(BASE_DIR, 'file/upload/') make_dir(upload_dir, force=False, backup=False) this_path = upload_dir + filename result = path_isexists(upload_dir) if not result[0]: return (False, '上传目录不存在') with open(this_path, 'wb+') as dest: for chunk in file.chunks(): dest.write(chunk) os.system('chmod ' + str(chmods) + ' ' + this_path) return (True, this_path) return (False, '上传失败')
def uncompress(src_file, dest_dir): result = get_filetype(src_file) if not result[0]: return (False, result[1], '') filefmt = result[1] result = make_dir(dest_dir) if not result: return (False, '创建解压目录失败', filefmt) if filefmt in ('tgz', 'tar'): try: tar = tarfile.open(src_file) names = tar.getnames() for name in names: tar.extract(name, dest_dir) tar.close() except Exception as e: return (False, e, filefmt) elif filefmt == 'zip': try: zip_file = zipfile.ZipFile(src_file) for names in zip_file.namelist(): zip_file.extract(names, dest_dir) zip_file.close() except Exception as e: return (False, e, filefmt) elif filefmt == 'rar': try: rar = rarfile.RarFile(src_file) os.chdir(dest_dir) rar.extractall() rar.close() except Exception as e: return (False, e, filefmt) elif filefmt == 'gz': try: f_name = dest_dir + '/' + os.path.basename(src_file) # 获取文件的名称,去掉 g_file = gzip.GzipFile(src_file) # 创建gzip对象 open(f_name, "w+").write(g_file.read()) # gzip对象用read()打开后,写入open()建立的文件中。 g_file.close() # 关闭gzip对象 result = get_filetype(src_file) if not result[0]: new_filefmt = '未知' else: new_filefmt = result[1] return (True, '解压后的文件格式为:' + new_filefmt, filefmt) except Exception as e: return (False, e, filefmt) else: return (False, '文件格式不支持或者不是压缩文件', filefmt) return (True, '', filefmt)
def uncompress(src_file, dest_dir): result = get_filetype(src_file) if not result[0] : return (False, result[1], '') filefmt = result[1] result = make_dir(dest_dir) if not result : return (False, '创建解压目录失败', filefmt) if filefmt in ('tgz', 'tar') : try : tar = tarfile.open(src_file) names = tar.getnames() for name in names: tar.extract(name, dest_dir) tar.close() except Exception as e : return (False, e, filefmt) elif filefmt == 'zip': try : zip_file = zipfile.ZipFile(src_file) for names in zip_file.namelist(): zip_file.extract(names, dest_dir) zip_file.close() except Exception as e : return (False, e, filefmt) elif filefmt == 'rar' : try : rar = rarfile.RarFile(src_file) os.chdir(dest_dir) rar.extractall() rar.close() except Exception as e : return (False, e, filefmt) elif filefmt == 'gz' : try : f_name = dest_dir + '/' + os.path.basename(src_file) # 获取文件的名称,去掉 g_file = gzip.GzipFile(src_file) # 创建gzip对象 open(f_name, "w+").write(g_file.read()) # gzip对象用read()打开后,写入open()建立的文件中。 g_file.close() # 关闭gzip对象 result = get_filetype(src_file) if not result[0] : new_filefmt = '未知' else : new_filefmt = result[1] return (True, '解压后的文件格式为:' + new_filefmt, filefmt) except Exception as e : return (False, e, filefmt) else : return (False, '文件格式不支持或者不是压缩文件', filefmt) return (True, '', filefmt)
def write_file(file , mode , content, force=False , backup=True): ''' 用于把指定内容写入文件 :parm file:写入文件 mode:打开文件模式,必须为写 content:写入文件内容 force:当文件存在,强制执行 backup:当文件存在,备份文件,后缀名为%Y%m%d%H%M%S-bk :return True:成功 Flase:失败 ''' if 'w' not in mode and 'a' not in mode and '+' not in mode : return (False, '参数mode只能含有写模式(必须含有w、a、+)') if not isinstance(file, str) : return (False, '参数file只能为字符串') if os.path.exists(file) : if os.path.isdir(file) : return (False, file + '是一个存在的目录') else : if not 'a' in mode : if not force : return (False, file + '文件存在') if backup == True : try : dt = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time())) pre_result = os.renames(file , file + '-' + str(dt) + '-' + 'bk') except Exception as e : pre_result = str(e) else : pre_result = False else : pre_result = False else : dirname = os.path.dirname(file) if os.path.exists(dirname) : if not os.path.isdir(dirname) : return (False, '目录为文件') else : mkresult = make_dir(dirname) if not mkresult : return (False, '无法创建目录') pre_result = False content = bytes2string(content) content = str(content) if not pre_result : try : pf = open(file, mode) pf.writelines(content + '\n') pf.close() return (True, file) except Exception as e : rescode = False result = str(e) else : rescode = False result = '未知错误' return (rescode, result)
def uncompress(src_file, dest_dir): result = get_filetype(src_file) if not result[0]: return (False, result[1], '') filefmt = result[1] if filefmt not in ('zip'): return (False, '本系统暂时只支持解压zip格式的文件') result = make_dir(dest_dir) if not result: return (False, '创建解压目录失败', filefmt) if filefmt in ('tgz', 'tar'): try: tar = tarfile.open(src_file) names = tar.getnames() for name in names: tar.extract(name, dest_dir) tar.close() except Exception as e: return (False, e, filefmt) elif filefmt == 'zip': try: zip_file = zipfile.ZipFile(src_file) for names in zip_file.namelist(): zip_file.extract(names, dest_dir) zip_file.close() except Exception as e: return (False, e, filefmt) elif filefmt == 'rar': try: rar = rarfile.RarFile(src_file) os.chdir(dest_dir) rar.extractall() rar.close() except Exception as e: if str(e) == "Unrar not installed? (rarfile.UNRAR_TOOL='unrar')": return (False, '请在服务器上安装unrar,yum install -y unrar', filefmt) return (False, e, filefmt) elif filefmt == 'gz': try: f_name = dest_dir + '/' + os.path.basename(src_file) # 获取文件的名称,去掉 g_file = gzip.GzipFile(src_file) # 创建gzip对象 open(f_name, "w+").write(g_file.read()) # gzip对象用read()打开后,写入open()建立的文件中。 g_file.close() # 关闭gzip对象 result = get_filetype(src_file) if not result[0]: new_filefmt = '未知' else: new_filefmt = result[1] return (True, '解压后的文件格式为:' + new_filefmt, filefmt) ''' suffix = os.path.splitext(src_file)[-1] if suffix : f_name = src_file.replace(suffix, "") else : f_name = src_file + '_' + random_str() #获取文件的名称,去掉 g_file = gzip.GzipFile(src_file) #创建gzip对象 open(f_name, "w+").write(g_file.read()) #gzip对象用read()打开后,写入open()建立的文件中。 g_file.close() #关闭gzip对象 result = get_filetype(src_file) if not result[0] : return (False, result[1], '') new_filefmt = result[1] return (True, '解压后的文件格式为:' + new_filefmt, filefmt) result = uncompress(f_name, dest_dir) if not result[0] : new_filefmt = result[2] if new_filefmt not in ('gz','tar','tgz','zip' ,'rar') : return (True, '解压后的文件格式为:' + new_filefmt, filefmt) else : return (False, '解压后的文件格式不支持或者不是压缩文件', filefmt) ''' except Exception as e: return (False, e, filefmt) else: return (False, '文件格式不支持或者不是压缩文件', filefmt) return (True, '', filefmt)