在循环中,可选地将输出写入文件
我编写了一个迭代性计算一些数量x,y
的函数,以返回最终结果。此外,此代码将x,y
的每次迭代保存到文件中。这是基本结构:
def myFunc():
X,Y = 0,0
file1 = open(output1,"w")
file2 = open(output2,"w")
for i in range(1000):
X,Y = someCalculation(X,Y) #calculations happen here
file1.write(X)
file2.write(Y)
file1.close()
file2.close()
return X,Y
但是,如果文件名 或output2
在调用函数时省略了,我需要此函数才能执行相同的计算而无需附加相关文件的任何内容。
这是我凌乱的解决方案:
def myFunc(output1=None,output2=None):
X,Y = 0,0
if (output1 != None): file1 = open(output1,"w")
if (output2 != None): file2 = open(output2,"w")
for i in range(1000):
X,Y = someCalculation(X,Y) #calculations happen here
if (output1 != None): file1.write(X)
if (output2 != None): file2.write(Y)
if (output1 != None): file1.close()
if (output2 != None): file2.close()
return X,Y
是否有一种更好,更干净的写作方式?
I wrote a function which iteratively computes some quantities X,Y
, returning the final result. Additionally, this code saves each iteration of X,Y
to a file. Here is the basic structure:
def myFunc():
X,Y = 0,0
file1 = open(output1,"w")
file2 = open(output2,"w")
for i in range(1000):
X,Y = someCalculation(X,Y) #calculations happen here
file1.write(X)
file2.write(Y)
file1.close()
file2.close()
return X,Y
However, if the filename output1
or output2
is omitted when the function is called, I need this function to perform the same calculation without appending anything to the relevant file.
Here is my messy solution:
def myFunc(output1=None,output2=None):
X,Y = 0,0
if (output1 != None): file1 = open(output1,"w")
if (output2 != None): file2 = open(output2,"w")
for i in range(1000):
X,Y = someCalculation(X,Y) #calculations happen here
if (output1 != None): file1.write(X)
if (output2 != None): file2.write(Y)
if (output1 != None): file1.close()
if (output2 != None): file2.close()
return X,Y
Is there a better, cleaner way to write this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
制作一个虚拟文件对象,忽略写入并支持上下文管理器接口:
创建一个辅助函数,当文件名 none 时,创建其中一个而不是普通文件:
使用代码>块来管理文件寿命,就像您应该做的那样 - 但是现在使用
my_open
而不是open
:Make a dummy file object that ignores writes, and supports the context manager interface:
Make a helper function that creates one of these instead of a normal file when the filename
is None
:Use
with
blocks to manage the file lifetimes, as you should do anyway - but now usemy_open
instead ofopen
:您可以创建一个虚拟类,该类没有do <代码>写方法。
ExitStack
用于确保自动关闭任何打开的文件。当您似乎正在记录
x
和/或y
值时,您可能需要考虑使用logging
模块,为适当的记录器创建fileHandler
,而不是将输出文件名称为myFunc
本身。You can create a dummy class that has a do-nothing
write
method.ExitStack
is used to ensure any opened files are closed automatically.As you appear to be logging the
X
and/orY
values at each step, you may want to look into using thelogging
module instead, creating aFileHandler
for the appropriate logger instead of passing output file names tomyFunc
itself.简单:写入操作系统定义的“空设备”文件,以免写信给。
标准库
OS
模块定义字符串常数devNull
,它标识了此文件的路径:'/dev/null'
Mac and Linux上'nul'
在Windows上(我想后者实际上不是路径)。Simpler: write to a "null device" file defined by your operating system to have no effect when written to.
The standard library
os
module defines a string constantdevnull
which identifies the path to this file:'/dev/null'
on Mac and Linux,'NUL'
on Windows (I suppose the latter is not actually a path).