Import numpy AS NP Import OS, SYS # Get the current folder, and the file name DEF path (fileName): P = the sys.path [0] + ' \\ ' + fileName return P # read file DEF readFile (fileName): F = Open (path (fileName)) STR = reached, f.read () f.close () return STR # write file DEF the writeFile (fileName, STR): F = Open (path (fileName), ' W ' ) f.write (str) f.close () DEF str1 (): str = ' , ' .join ( ' I am proud to be grown on the land of China! ' ) return str DEF str2 (): return str (np.random.randint (-49,50, [3, 3,3 ])) # experiment. 1 DEF TEST_1 (): fileName = ' China land .txt ' the writeFile (fileName, str1 ()) List = readFile (fileName) .split ( ' , ' ) Print (List) # experiment 2 DEF test_2 (): the writeFile ( 'str1',str1()) writeFile('str2',str2()) str_1=readFile('str1') str_2=readFile('str2') print(str_1) print(str_2) test_2()
Knowledge points to explain: Python Series - File operations