我们从Python开源项目中,提取了以下35个代码示例,用于说明如何使用tarfile.write()。
def w2p_pack(filename, path, compiled=False, filenames=None): """Packs a web2py application. Args: filename(str): path to the resulting archive path(str): path to the application compiled(bool): if `True` packs the compiled version filenames(list): adds filenames to the archive """ filename = abspath(filename) path = abspath(path) tarname = filename + '.tar' if compiled: tar_compiled(tarname, path, '^[\w\.\-]+$', exclude_content_from=['cache', 'sessions', 'errors']) else: tar(tarname, path, '^[\w\.\-]+$', filenames=filenames, exclude_content_from=['cache', 'sessions', 'errors']) w2pfp = gzopen(filename, 'wb') tarfp = open(tarname, 'rb') w2pfp.write(tarfp.read()) w2pfp.close() tarfp.close() os.unlink(tarname)
def w2p_unpack(filename, path, delete_tar=True): if filename == 'welcome.w2p': create_welcome_w2p() filename = abspath(filename) path = abspath(path) if filename[-4:] == '.w2p' or filename[-3:] == '.gz': if filename[-4:] == '.w2p': tarname = filename[:-4] + '.tar' else: tarname = filename[:-3] + '.tar' fgzipped = gzopen(filename, 'rb') tarfile = open(tarname, 'wb') tarfile.write(fgzipped.read()) tarfile.close() fgzipped.close() else: tarname = filename untar(tarname, path) if delete_tar: os.unlink(tarname)
def copystream( src, dest, size, chunk_size=10 ** 5, ): """ this is here because I think there is a bug in shutil.copyfileobj """ while size > 0: if size < chunk_size: data = src.read(size) else: data = src.read(chunk_size) length = len(data) if length > size: (data, length) = (data[:size], size) size -= length if length == 0: break dest.write(data) if length < chunk_size: break dest.seek(0) return
def w2p_pack(filename, path, compiled=False, filenames=None): """Packs a web2py application. Args: filename(str): path to the resulting archive path(str): path to the application compiled(bool): if `True` packs the compiled version filenames(list): adds filenames to the archive """ filename = abspath(filename) path = abspath(path) tarname = filename + '.tar' if compiled: tar_compiled(tarname, path, '^[\w\.\-]+$') else: tar(tarname, path, '^[\w\.\-]+$', filenames=filenames) w2pfp = gzopen(filename, 'wb') tarfp = open(tarname, 'rb') w2pfp.write(tarfp.read()) w2pfp.close() tarfp.close() os.unlink(tarname)
def w2p_unpack(filename, path, delete_tar=True): if filename=='welcome.w2p': create_welcome_w2p() filename = abspath(filename) path = abspath(path) if filename[-4:] == '.w2p' or filename[-3:] == '.gz': if filename[-4:] == '.w2p': tarname = filename[:-4] + '.tar' else: tarname = filename[:-3] + '.tar' fgzipped = gzopen(filename, 'rb') tarfile = open(tarname, 'wb') tarfile.write(fgzipped.read()) tarfile.close() fgzipped.close() else: tarname = filename untar(tarname, path) if delete_tar: os.unlink(tarname)
def write_file(filename, value, mode='w'): """Writes <value> to filename, making sure to close the file explicitly on exit. """ f = open_file(filename, mode) try: return f.write(value) finally: f.close()
def make_fake_file_like_object(): class LogFile(object): def write(self, value): pass def close(self): pass return LogFile()
def write_file(filename, value, mode='w'): """Writes <value> to filename, making sure to close the file explicitly on exit. """ f = open(filename, mode) try: return f.write(value) finally: f.close()