我只是编程的初学者,下面是我用python编写的代码,用于保存和编辑文件,但每次运行程序时都会删除以前的保存数据,所以我很困惑为什么呢?'发生了什么?
filename = raw_input("Please enter the file name to open it:\n")
doc = open (filename,'w')
print doc.read
text_input = raw_input("Please enter the data you want to enter in file:\n")
if text_input == "":
print "no input closing the programme."
else :
doc.write(text_input)
doc.close()
print "Printing the file:\n"
print doc.read
cl_file = raw_input("do you want to truncate file(y/n): ")
if cl_file == "y":
doc.truncate()
else :
print "Wrong input closing notepad"
exit()
答案 0 :(得分:1)
您正在以写入模式打开文件,该文件在写入文件之前截断该文件。而不是使用export Class ChildComponent implements OnInit{
arrayToGet; //An array that I want to pass from Parent to child
ngOnInit(){
console.log('The Array I got is ', this.arrayToGet); //Undefined, Tried even with setTimeout
}
@Input('arrayToGet')
set _arrayToGet(data: Array) {
this.arrayToGet = data;
console.log(this.arrayToGet);
}
//A bunch of methods to work with the array I get
}
使用open(filename, 'w')
。 'a'值告诉open函数使用append模式,以便将对文件的写入添加到任何现有内容的末尾。