python的writeline()方法问题
fromsysimportargvimportsys,osdefwrite_file(string1,string2):txtfile=raw_input("please...
from sys import argv
import sys,os
def write_file(string1,string2):
txtfile=raw_input("please input create name of file:")
txt_tmp=open(txtfile,'rw')
txt_tmp.truncate()
txt_tmp.write(string1)
txt_tmp.write(string2)
txt_tmp.write("\nover!")
#txt_tmp.close()
#load file and print
#txt_read=open(txtfile,'r')
txt_tmp.seek(0)
tmp_put=txt_tmp.readline()#input one line
print("begin input file:")
sys.stdout.write("%s\n" %tmp_put)
print(txt_tmp.readline())
print(tmp_put)#input all
txt_tmp.close()
print("methon begin:")
#write_string(raw_input("please input a string:"),raw_input("please input anthor string:"))
#write_two('haha',"hello world\n")
#write_print()
write_file("ssss","xxxxx")
编译运行后 报:
Traceback (most recent call last):
File "function.py", line 39, in <module>
write_file('ssss','xxxxx')
File "function.py", line 16, in write_file
txt_tmp=open(txtfile,'rw')
ValueError: Invalid mode ('rw')
请问这是什么问题呀?没有积分了,谢谢 展开
import sys,os
def write_file(string1,string2):
txtfile=raw_input("please input create name of file:")
txt_tmp=open(txtfile,'rw')
txt_tmp.truncate()
txt_tmp.write(string1)
txt_tmp.write(string2)
txt_tmp.write("\nover!")
#txt_tmp.close()
#load file and print
#txt_read=open(txtfile,'r')
txt_tmp.seek(0)
tmp_put=txt_tmp.readline()#input one line
print("begin input file:")
sys.stdout.write("%s\n" %tmp_put)
print(txt_tmp.readline())
print(tmp_put)#input all
txt_tmp.close()
print("methon begin:")
#write_string(raw_input("please input a string:"),raw_input("please input anthor string:"))
#write_two('haha',"hello world\n")
#write_print()
write_file("ssss","xxxxx")
编译运行后 报:
Traceback (most recent call last):
File "function.py", line 39, in <module>
write_file('ssss','xxxxx')
File "function.py", line 16, in write_file
txt_tmp=open(txtfile,'rw')
ValueError: Invalid mode ('rw')
请问这是什么问题呀?没有积分了,谢谢 展开
2个回答
推荐于2017-10-12 · 知道合伙人互联网行家
关注
展开全部
Python中函数 writelines(list)为写入方式,函数writelines可以将list写入到文件中,但是不会在list每个元素后加换行符,所以如果想每行都有换行符的话需要自己再加上。
sample_list = [line+'\n' for line in sample_list] #\n换行符(转义字符)
outfile.wirtelines(sample_list)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询