Python Learning note 6-Input output and file read Write

Source: Internet
Author: User

(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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.