from io import open #定義字串 s = "123456789\n" #開啟test.txt f = open("test.txt", "w+") #定義List l = [] #輸出資料 for i in range(1,11): #加入到List l.append(s) f.write(s) f.flush() #將List輸出到text.txt f.writelines(l) f.flush() #c關閉檔案 f.close() #開啟檔案並印出 with open("test.txt", "r+") as a: print(a.readlines()) a.close() #開啟檔案並印出 f = open("test.txt", "r+") for i in f.readlines(): print(i, end='') f.close()
參考資料:
http://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files
http://docs.python.org/3/library/functions.html#open
http://docs.python.org/3/library/io.html
http://docs.python.org/3/glossary.html#term-text-file
http://docs.python.org/3/library/importlib.html#importlib.abc.Finder
http://docs.python.org/3/reference/compound_stmts.html#with