我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用tarfile.close()。
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_pack_plugin(filename, path, plugin_name): """Packs the given plugin into a w2p file. Will match files at:: <path>/*/plugin_[name].* <path>/*/plugin_[name]/* """ filename = abspath(filename) path = abspath(path) if not filename.endswith('web2py.plugin.%s.w2p' % plugin_name): raise Exception("Not a web2py plugin name") plugin_tarball = tarfile.open(filename, 'w:gz') try: app_dir = path while app_dir[-1] == '/': app_dir = app_dir[:-1] files1 = glob.glob( os.path.join(app_dir, '*/plugin_%s.*' % plugin_name)) files2 = glob.glob( os.path.join(app_dir, '*/plugin_%s/*' % plugin_name)) for file in files1 + files2: plugin_tarball.add(file, arcname=file[len(app_dir) + 1:]) finally: plugin_tarball.close()
def tar_compiled(file, dir, expression='^.+$', exclude_content_from=None): """Used to tar a compiled application. The content of models, views, controllers is not stored in the tar file. """ tar = tarfile.TarFile(file, 'w') for file in listdir(dir, expression, add_dirs=True, exclude_content_from=exclude_content_from): filename = os.path.join(dir, file) if os.path.islink(filename): continue if os.path.isfile(filename) and file[-4:] != '.pyc': if file[:6] == 'models': continue if file[:5] == 'views': continue if file[:11] == 'controllers': continue if file[:7] == 'modules': continue tar.add(filename, file, False) tar.close()
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 tar_compiled(file, dir, expression='^.+$'): """Used to tar a compiled application. The content of models, views, controllers is not stored in the tar file. """ tar = tarfile.TarFile(file, 'w') for file in listdir(dir, expression, add_dirs=True): filename = os.path.join(dir, file) if os.path.islink(filename): continue if os.path.isfile(filename) and file[-4:] != '.pyc': if file[:6] == 'models': continue if file[:5] == 'views': continue if file[:11] == 'controllers': continue if file[:7] == 'modules': continue tar.add(filename, file, False) tar.close()
def read_file(filename, mode='r'): """Returns content from filename, making sure to close the file explicitly on exit. """ f = open_file(filename, mode) try: return f.read() finally: f.close()
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 _extractall(filename, path='.', members=None): tar = tarfile.TarFile(filename, 'r') ret = tar.extractall(path, members) tar.close() return ret
def tar(file, dir, expression='^.+$', filenames=None, exclude_content_from=None): """Tars dir into file, only tars file that match expression """ tar = tarfile.TarFile(file, 'w') try: if filenames is None: filenames = listdir(dir, expression, add_dirs=True, exclude_content_from=exclude_content_from) for file in filenames: tar.add(os.path.join(dir, file), file, False) finally: tar.close()
def make_fake_file_like_object(): class LogFile(object): def write(self, value): pass def close(self): pass return LogFile()
def read_file(filename, mode='r'): """Returns content from filename, making sure to close the file explicitly on exit. """ f = open(filename, mode) try: return f.read() finally: f.close()
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()