(1) Print to screen: print
(2) Read keyboard input: Input/raw_input
1 #Keyboard Input2str = Raw_input ("Please enter:");3 Print("What you have entered is:", str)4 5str = input ("Please enter:");6 Print("What you have entered is:", str)
(3) Opening the file open closes close reading read
1 #open and close files2 #Open a file3FO = open ("foo.txt","WB")4 Print("file name:", Fo.name)5 Print("is closed:", fo.closed)6 Print("access mode:", Fo.mode)#WB7 Print("whether to force a space at the end:", Fo.softspace)
1 #open a file that does not exist to create2FO = open ("foo.txt","WB")3 Print("file name:", Fo.name)4 fo.close ()5 6 #Open a file7FO = open ("foo.txt","WB")8Fo.write ("www.runoob.com!\nvery Good site!\n");9 Ten #Close an open file One fo.close () A - - #Open a file theFO = open ("foo.txt","r+") -str = Fo.read (10); - Print("the string to read is:", str) - #Close an open file + fo.close () - + A #Open a file atFO = open ("foo.txt","r+") -str = Fo.read (10); - Print("the string to read is:", str)
(4) Find location define pointer position
1 #Find Current Location2Position =Fo.tell ();3 Print("Current file location:", Position)4 5 #Reposition The pointer again to the beginning of the file6Position =fo.seek (0, 0);7str = Fo.read (10);8 Print("To re-read the string:", str)9 #Close an open fileTenFo.close ()
(5) file renaming and deletion
1 ImportOS2 3 #Rename the file Test1.txt to Test2.txt. 4Os.rename ("Test1.txt","Test2.txt" )5 6 7 ImportOS8 9 #Delete a file that already exists Test2.txtTenOs.remove ("Test2.txt")
Python Learning note 6-Input output and file read Write