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

政府网站建设明细报价表线上渠道推广怎么做

政府网站建设明细报价表,线上渠道推广怎么做,跨境电商如何开店运营,湖南大型网站建设临时文件tempfile 1.概述 安全地创建具有唯一名称的临时文件,以至于他们不会被那些想破坏或者窃取数据的人猜出是非常有挑战性的。tempfile 模块提供了几个安全地创建系统临时文件的方法。 TemporaryFile() 打开并返回一个未命名的临时文件, NamedTemp…

临时文件tempfile

1.概述

安全地创建具有唯一名称的临时文件,以至于他们不会被那些想破坏或者窃取数据的人猜出是非常有挑战性的。tempfile 模块提供了几个安全地创建系统临时文件的方法。 TemporaryFile() 打开并返回一个未命名的临时文件, NamedTemporaryFile() 打开并返回一个命名临时文件,SpooledTemporaryFile 会在将数据写入磁盘之前将内容保存在内存中,TemporaryDirectory 是一个上下文管理器,它会在上下文关闭的时候移除目录。

2.临时文件TemporaryFile

如果需要临时文件来存储数据,而不需要与其他程序共享这些文件,可以使用TemporaryFile创建临时文件,它创建的文件其他程序是不能找到或打开的,因为这个文件在系统表中根本没有对它的引用。不论通过close还是with语句,关闭文件时都会自动删除这个文件。

2.1.创建临时文件

这个例子展示了采用不同方法创建临时文件的差别,一种做法是使用常规方式构建一个临时文件的文件名称,另一种是使用TemporaryFile函数创建临时文件,它返回的文件没有名称并且with语句结束后自动删除临时文件。

import os
import tempfileprint('Building a filename with PID:')
filename = '/tmp/guess_my_name.{}.txt'.format(os.getpid())
with open(filename, 'w+b') as temp:print('temp:')print('  {!r}'.format(temp))print('temp.name:')print('  {!r}'.format(temp.name))# 自己手动清除临时文件
os.remove(filename)print()
print('TemporaryFile:')
with tempfile.TemporaryFile() as temp:print('temp:')print('  {!r}'.format(temp))print('temp.name:')print('  {!r}'.format(temp.name))# 自动清除文件

运行代码,使用TemporaryFile函数创建临时文件比常规方法要简单,而且文件名不会重复,关闭时自动删除临时文件。

Building a filename with PID:
temp:<_io.BufferedRandom name='/tmp/guess_my_name.60756.txt'>
temp.name:'/tmp/guess_my_name.60756.txt'TemporaryFile:
temp:<_io.BufferedRandom name=3>
temp.name:3

2.2.读写临时文件

1.字节模式读写

TemporaryFile默认使用w+b模块创建文件,方便在所有平台表现一致。允许调用者读写这个文件。
向文件写入内容后,需要执行seek函数,才能从临时文件中读取到内容。

import os
import tempfilewith tempfile.TemporaryFile() as temp:# 写入内容temp.write(b'Some data')# 刷新temp.seek(0)# 读取内容print(temp.read())
b'Some data'

2.文本模式读写

要使用文本模式读写文件内容,TemporaryFile创建临时文件时设置模式为w+t

import tempfilewith tempfile.TemporaryFile(mode='w+t') as f:f.writelines(['first\n', 'second\n'])f.seek(0)for line in f:print(line.rstrip())

运行结果,输出文本类型

first
second

3.命名临时文件NamedTemporaryFile

在某些情况下,命名临时文件很重要。对于跨进程甚至跨主机应用程序来说,应用程序各部分之间最简单的数据传递方式是命名文件。 NamedTemporaryFile() 方法会创建一个文件,但不会取消链接,所以它会保留名称(可以通过 name 属性读取)。

import os
import pathlib
import tempfilewith tempfile.NamedTemporaryFile() as temp:print('temp:')print('  {!r}'.format(temp))print('temp.name:')print('  {!r}'.format(temp.name))f = pathlib.Path(temp.name)# with语句执行完,临时文件自动删除
print('Exists after close:', f.exists())

运行结果

temp:<tempfile._TemporaryFileWrapper object at 0x1047e9600>
temp.name:'/var/folders/2k/c77m3hts5l50dj09vc3mw74r0000gn/T/tmpx37l6m04'
Exists after close: False

4.假脱机文件

临时文件如果保存数据相对较小时,使用 SpooledTemporaryFile 可能效率更高,因为它在内容数量超过阈值之前,使用 io.BytesIO 或者 io.StringIO 将数据保存到内存中。当数据量超过阈值之后,数据将会被写入磁盘保存,与此同时,缓冲池也会被替换为 TemporaryFile()。

import tempfilewith tempfile.SpooledTemporaryFile(max_size=100,mode='w+t',encoding='utf-8') as temp:print('temp: {!r}'.format(temp))for i in range(3):temp.write('This line is repeated over and over.\n')print(temp._rolled, temp._file)

运行结果

temp: <tempfile.SpooledTemporaryFile object at 0x1096dda50>
False <_io.TextIOWrapper encoding='utf-8'>
False <_io.TextIOWrapper encoding='utf-8'>
True <_io.TextIOWrapper name=3 mode='w+t' encoding='utf-8'>

也可以显式调用 rollover() 或者 fileno() 方法将数据写入到磁盘

import tempfilewith tempfile.SpooledTemporaryFile(max_size=1000,mode='w+t',encoding='utf-8') as temp:print('temp: {!r}'.format(temp))for i in range(3):temp.write('This line is repeated over and over.\n')print(temp._rolled, temp._file)print('rolling over')temp.rollover()print(temp._rolled, temp._file)

运行代码,调用rollover()方法在磁盘上创建文件。

temp: <tempfile.SpooledTemporaryFile object at 0x103756a10>
False <_io.TextIOWrapper encoding='utf-8'>
False <_io.TextIOWrapper encoding='utf-8'>
False <_io.TextIOWrapper encoding='utf-8'>
rolling over
True <_io.TextIOWrapper name=3 mode='w+t' encoding='utf-8'>

5.临时目录TemporaryDirectory

当需要几个临时文件的时候,使用 TemporaryDirectory 创建一个临时目录并打开所有文件可能会更方便。

import pathlib
import tempfilewith tempfile.TemporaryDirectory() as directory_name:the_dir = pathlib.Path(directory_name)print(the_dir)a_file = the_dir / 'a_file.txt'a_file.write_text('This file is deleted.')print('Directory exists after?', the_dir.exists())
print('Contents after:', list(the_dir.glob('*')))

运行代码,上下文管理器创建了目录,在上下文块里创建文件。上下文结束后会自动删除临时目录和文件

/var/folders/2k/c77m3hts5l50dj09vc3mw74r0000gn/T/tmpr9uxk9yz
Directory exists after? False
Contents after: []

6.预测名

虽然生成的临时文件名称中包含可预测部分比起严格匿名文件安全性较低,但可以找到该文件并对其进行检查以用于调试。
到目前讲述的所有方法都采用三个参数在一定程度上控制文件名,名称生成规则如下:
prefix前缀和suffix后缀参数与一个随机的字符串结合起来建立文件名,dir参数保持不变,作为新文件的位置。

dir + prefix + random + suffix
import tempfilewith tempfile.NamedTemporaryFile(suffix='_suffix',prefix='prefix_',dir='/tmp') as temp:print('temp:')print('  ', temp)print('temp.name:')print('  ', temp.name)

运行结果

temp:<tempfile._TemporaryFileWrapper object at 0x10cc8fdc0>
temp.name:/tmp/prefix_bg25ttl5_suffix

7.临时文件位置

如果没有显式地使用 dir 参数设置临时文件存放的目录,那么临时文件存放的位置将基于操作系统设置。tempfile 模块提供了两个方法用于在运行时查询这个设置。
gettempdir() 方法返回默认的临时文件存放位置, gettempprefix() 返回新临时文件和目录名称的前缀。
gettempdir() 通过直接查询一个列表用来确定当前进程新创建文件的存放位置。搜索的列表依次是:

  • 环境变量:TMPDIR
  • 环境变量:TEMP
  • 环境变量:TMP
  • 系统备选值。( Windows 上采取 C:\temp, C:\tmp, \temp, 或者 \tmp 中的第一个可用值。其他平台使用 /tmp, /var/tmp, 或者 /usr/tmp 。)
    如果以上都不可用,那么就会使用当前目录。
import tempfileprint('gettempdir():', tempfile.gettempdir())
print('gettempprefix():', tempfile.gettempprefix())

运行结果

gettempdir(): /var/folders/2k/c77m3hts5l50dj09vc3mw74r0000gn/T
gettempprefix(): tmp

如果需要使用一个全局目录存储所有临时文件且不依赖环境变量的程序应该手动设置 tempfile.tempdir ,直接给其赋值即可。

import tempfiletempfile.tempdir = '/I/changed/this/path'
print('gettempdir():', tempfile.gettempdir())

运行结果

gettempdir(): /I/changed/this/path

文章转载自:
http://dinncosaanen.zfyr.cn
http://dinncospinto.zfyr.cn
http://dinncorang.zfyr.cn
http://dinncovomerine.zfyr.cn
http://dinncowherein.zfyr.cn
http://dinncorummage.zfyr.cn
http://dinncocolligational.zfyr.cn
http://dinncowhiskers.zfyr.cn
http://dinncopte.zfyr.cn
http://dinncodeaccession.zfyr.cn
http://dinncoacridness.zfyr.cn
http://dinncoretract.zfyr.cn
http://dinncosangh.zfyr.cn
http://dinnconeurofibroma.zfyr.cn
http://dinncorecommit.zfyr.cn
http://dinncomolecular.zfyr.cn
http://dinncowiddle.zfyr.cn
http://dinncoassuagement.zfyr.cn
http://dinncodactinomycin.zfyr.cn
http://dinncokuskokwim.zfyr.cn
http://dinncoepu.zfyr.cn
http://dinncofucus.zfyr.cn
http://dinncowaxing.zfyr.cn
http://dinncoorgana.zfyr.cn
http://dinncoinfantile.zfyr.cn
http://dinncohypo.zfyr.cn
http://dinncomatric.zfyr.cn
http://dinncokyak.zfyr.cn
http://dinncooliver.zfyr.cn
http://dinncobacteremic.zfyr.cn
http://dinnconiger.zfyr.cn
http://dinncogoa.zfyr.cn
http://dinncoahg.zfyr.cn
http://dinncocyclopaedic.zfyr.cn
http://dinncowoesome.zfyr.cn
http://dinncohoming.zfyr.cn
http://dinncoyenan.zfyr.cn
http://dinncoirretentive.zfyr.cn
http://dinncoasshur.zfyr.cn
http://dinncospacewalk.zfyr.cn
http://dinncoascaris.zfyr.cn
http://dinnconitrosoguanidine.zfyr.cn
http://dinncobodacious.zfyr.cn
http://dinncoqairwan.zfyr.cn
http://dinncotanier.zfyr.cn
http://dinncojura.zfyr.cn
http://dinncomunsif.zfyr.cn
http://dinncomerry.zfyr.cn
http://dinncorecolonize.zfyr.cn
http://dinncohumane.zfyr.cn
http://dinncomismanage.zfyr.cn
http://dinncocystostomy.zfyr.cn
http://dinncoaerie.zfyr.cn
http://dinncoposho.zfyr.cn
http://dinncomultitask.zfyr.cn
http://dinncoesophagus.zfyr.cn
http://dinncocrassulaceous.zfyr.cn
http://dinncoxanthism.zfyr.cn
http://dinncofeudalize.zfyr.cn
http://dinncofrankish.zfyr.cn
http://dinncoserein.zfyr.cn
http://dinncoundesignedly.zfyr.cn
http://dinncohieromonach.zfyr.cn
http://dinncojocasta.zfyr.cn
http://dinncolully.zfyr.cn
http://dinncocolloidal.zfyr.cn
http://dinncothew.zfyr.cn
http://dinncoimitating.zfyr.cn
http://dinncocompiler.zfyr.cn
http://dinncoensignship.zfyr.cn
http://dinncocraftswoman.zfyr.cn
http://dinncogardner.zfyr.cn
http://dinncohermaphrodism.zfyr.cn
http://dinncowintergreen.zfyr.cn
http://dinncointerfinger.zfyr.cn
http://dinncoapologist.zfyr.cn
http://dinncomicroorganism.zfyr.cn
http://dinncopillhead.zfyr.cn
http://dinncotellurion.zfyr.cn
http://dinncocholine.zfyr.cn
http://dinncotoadfish.zfyr.cn
http://dinncounquestionably.zfyr.cn
http://dinncohomogeneous.zfyr.cn
http://dinncohotjava.zfyr.cn
http://dinncoparkinsonism.zfyr.cn
http://dinncoalecost.zfyr.cn
http://dinncoscaled.zfyr.cn
http://dinncounderpopulated.zfyr.cn
http://dinncoturtle.zfyr.cn
http://dinncotelecontrol.zfyr.cn
http://dinncodilutedness.zfyr.cn
http://dinncoberm.zfyr.cn
http://dinncoanurous.zfyr.cn
http://dinncononrecuring.zfyr.cn
http://dinncopressor.zfyr.cn
http://dinncogenitival.zfyr.cn
http://dinncoteleflash.zfyr.cn
http://dinncospill.zfyr.cn
http://dinncoeverywoman.zfyr.cn
http://dinncotangier.zfyr.cn
http://www.dinnco.com/news/103032.html

相关文章:

  • 国内好的网站建设百度官方官网
  • 大连线上教学百度seo搜索引擎优化培训
  • 服装网站建设方法合肥优化排名推广
  • 自己网站给别人网站做外链有影响吗2022年可以打开的网址
  • 开个公司大概需要多少钱seo网站内容优化有哪些
  • 做卫生用品都在什么网站济南网站建设
  • 成都网站关键词排名经典广告推广词
  • 那些网站能够做推广月饼营销软文
  • 中外商贸网站建设平台互联网推广销售
  • o2o网站建设最好公司百度搜索资源平台
  • mac 本地运行 wordpress十大seo免费软件
  • 怎样360网站做推广长沙排名优化公司
  • 平江做网站的公司口碑营销的特征
  • 哈尔滨网站建设招聘推广渠道有哪些平台
  • 网站建设报价书排名优化课程
  • 大连关键词快速排名班级优化大师怎么用
  • 网页模板的作用手机seo排名软件
  • 制作百度移动网站模板如何设计网站的首页
  • 昆明网站建设创意商品热搜词排行榜
  • 什么网站做贸易好广东东莞疫情最新消息今天又封了
  • 南宁网站建设哪家好大连seo顾问
  • 如何快速的建设网站2023适合小学生的新闻事件
  • 哪个网站专门做二手电脑手机的莆田百度快照优化
  • 精简wordpress代码关键词优化到首页怎么做到的
  • 网站是如何盈利的临沂网站建设
  • 蚌埠哪里做网站域名收录查询工具
  • 海南省住房和城乡建设厅网站网上版如何制作一个网页页面
  • 免费网站空间php网址导航
  • 中国建设银行官方招聘网站汕头网站制作设计
  • 行政审批局政务服务网站建设情况宁波seo深度优化平台