草庐IT

Handler_write

全部标签

python - 模拟调用 write()

我有一个需要将字符串写入文件的记录器类。所以,我有这样的方法:defwrite_to_file(self,string):self.__file_handle.write(string)请注意,错误处理已被删除。显然,我想在不写入文件的情况下进行测试。因此通过Mock进行模拟.我看过this这解释了如何模拟打开,但它在这里对我没有帮助——我在__init__中打开file_handle。现在,我可以在setUp()中执行此操作,但mock_open似乎在setUp之后超出了范围,因此在测试用例中没有用。您将如何使用Mock编写测试方法来测试write_to_file方法?

python - 使用 Python open().write() 将文件写入磁盘是否确保数据可用于其他进程?

一个Python进程将状态更新写入文件以供其他进程读取。在某些情况下,状态更新会在循环中重复且快速地发生。最简单和最快的方法是在一行中使用open().write():open(statusfile,'w').write(status)另一种方法是使用四行将数据强制写入磁盘。这会显着降低性能:f=open(self.statusfile,'w')f.write(status)os.fsync(f)f.close()我并不是要防止操作系统崩溃。那么,该方法是否将数据强制写入操作系统缓冲区,以便其他进程在从磁盘打开文件时读取最新的状态数据?或者,我需要使用os.fsync()吗?

python - 奇怪的 : logger only uses the formatter of the first handler for exceptions

我正在目睹日志记录模块以一种有趣的方式运行。我错过了什么吗?我正在做通常有两个处理程序的事情:一个StreamHandler仅用于将INFO和更高级别记录到控制台,另一个FileHandler也将处理所有DEBUG信息。在我决定为异常(exception)设置不同的格式之前,它一直运行良好。我想要文件中的完整堆栈跟踪,但只是控制台上的异常类型和值。由于处理程序具有setFormatter函数,而且编写logging.Formatter的子类似乎很容易,所以我认为它会起作用。控制台处理程序和文件处理程序都有自己的格式化程序。代码中的打印语句证明了这一点。但是,对logger.except

python - 如何将换行符添加到 file.write() 的末尾?

我有一个简单的python脚本,它输出到author.json文件。问题是它没有在文件末尾包含换行符。在author.json末尾添加换行符的最佳方法是什么?#!/usr/bin/envpythonimportjsonwithopen('input.json','r')ashandle:data=json.load(handle)output=open('author.json','w')author={}forkey,valueindata.items():ifkey=='id':author['id']=valueoutput.write(json.dumps(author,ind

python - "Expected type ' Union[str, bytearray] ' got ' int ' instead"write 方法警告

我的脚本使用预先生成的数据模式逐block写入文件:#Datapatterngeneratordefget_random_chunk_pattern():return''.join(random.choice(ascii_uppercase+digits+ascii_lowercase)for_inrange(8))....#DedupChunkclassCTOR:classDedupChunk:def__init__(self,chunk_size,chunk_pattern,chunk_position=0,state=DedupChunkStates.PENDING):self.

python - 操作系统错误 : raw write() returned invalid length when using print() in python

我正在使用pythontensorflow训练一个模型来识别python中的图像。但是当我尝试从github执行train.py时出现以下错误Traceback(mostrecentcalllast):File"train.py",line1023,intf.app.run(main=main,argv=[sys.argv[0]]+unparsed)File"C:\Users\sande\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\platform\app.py",line48,inrun_sys.exit

python - Django handler500 作为基于类的 View

为什么这行不通handler500=TemplateView.as_view(template_name="500.html")我得到以下异常:Traceback(mostrecentcalllast):File"/usr/lib/python2.6/wsgiref/handlers.py",line94,inrunself.finish_response()File"/usr/lib/python2.6/wsgiref/handlers.py",line134,infinish_responsefordatainself.result:File"/home/hatem/project

python - TypeError : write() argument must be str, 不是字节(Python 3 与 Python 2)

下面的代码完美适用于python2.7.13importoswithopen('random.bin','w')asf:f.write(os.urandom(10))但是对于python3会抛出错误3.6.0|python4.3.0(64位)|(默认,2016年12月23日,11:57:41)[MSCv.190064位(AMD64)]Traceback(mostrecentcalllast):File"C:/Users/hsingh/PycharmProjects/Item3.py",line3,inf.write(os.urandom(10))TypeError:write()arg

python - 为什么 sys.stdout.write ('\b' ) 不退格换行?

比较:foriteminrange(0,5):sys.stdout.write('c')foriteminrange(0,5):sys.stdout.write('\b')如您所想的那样工作,但是:foriteminrange(0,5):sys.stdout.write('\n')foriteminrange(0,5):sys.stdout.write('\b')仍然留给你五个换行符。有什么想法吗? 最佳答案 在今天看来,期望退格键能够在控制台上处理换行符似乎是合理的,但由于没有反向换行,因此不能向后兼容电传打字机。

python - Python3 中的 ElementTree TypeError "write() argument must be str, not bytes"

使用Python3和ElementTree生成.SVG文件时遇到问题。fromxml.etreeimportElementTreeasetdoc=et.Element('svg',width='480',height='360',version='1.1',xmlns='http://www.w3.org/2000/svg')#Doingthingswithetanddocf=open('sample.svg','w')f.write('\n')f.write('\n')f.write(et.tostring(doc))f.close()函数et.tostring(doc)生成类型错误