python,如何使用自定义函数来写进另一个文件

importsysdefprint_lol(the_list,indent=False,level=0,fh=sys.stdout):foreach_iteminthe_... import sys

def print_lol(the_list, indent=False, level=0, fh=sys.stdout):
for each_item in the_list:
if isinstance(each_item, list):
print_lol(each_item, indent, level+1, fh)
else:
if indent:
for tab_stop in range(level):
print("\t", end='', file=fh)
print(each_item, file=fh)

with open('man_data.txt') as mf :
print_lol(mf, True, 0, open("another.txt", "w"))

把man_data.txt写进another.txt文件,another.txt文件中格式都没变,没有一行行显示。
我觉得with那块行出问题了,如何写with那个句子?
若是这样写可以
man_data=['Is this the right room for an argument?', "No you haven't!", 'When?', "No you didn't!"]
print_lol(man_data, True, 0, open("another.txt", "w"))
输出:
another.txt中是

Is this the right room for an argument?
No you haven't!
When?
No you didn't!
展开
 我来答
matlab2000
2014-09-08 · TA获得超过2323个赞
知道大有可为答主
回答量:1678
采纳率:100%
帮助的人:1061万
展开全部
with无问题,你的open省略了模式后,默认为读r,当然没法写了。

open(name[, mode[,
buffering]])

Open a file, returning an object of the file
type described in section File Objects. If the file
cannot be opened, IOError
is raised. When opening a file, it’s preferable to use open()
instead of invoking the file constructor directly.

The first two arguments are the same as for stdio‘s fopen():
name is the file name to be opened, and mode is a string
indicating how the file is to be opened.

The most commonly-used values of mode are 'r' for reading, 'w' for writing (truncating
the file if it already exists), and 'a' for appending (which on some Unix systems
means that all writes append to the end of the file regardless of the
current seek position). 【If mode is omitted, it defaults to 'r'.】
更多追问追答
追问
with open('man_data.txt') as mf :

原来您说的是这里。
可是man_data.txt文件我又不动啊?
追答
open("man_data.txt","w“)不久可以了吗?
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式