【问题标题】:How to write one file in two different methods in python如何在python中以两种不同的方法编写一个文件
【发布时间】:2013-09-06 22:59:56
【问题描述】:

我试图用两种不同的方法只写一个文件。

有没有我可以在不创建另一个保存方法的情况下做到这一点?

【问题讨论】:

  • 将打开的文件传递给这两种方法?
  • 我正在使用 with open(file,'w')as f: 并且当涉及到第二种方法时它不会让我写。

标签: python file methods


【解决方案1】:

喜欢这样吗?

def method1(f):
   f.write("something")

def method2(f):
   f.write("something else")

with open("myfile", "w") as f:
    method1(f)
    method2(f)

【讨论】:

    猜你喜欢
    • 2014-06-22
    • 2020-07-24
    • 1970-01-01
    • 2022-11-01
    • 2020-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多