Working with files in Python

Source: Internet
Author: User
Tags file copy

Open File Mode process:

Open ----- edit ------ Close the elephant How to put into the refrigerator, do not tube how big, how to put, it is possible that the refrigerator is very large, the elephant is very small, directly open the refrigerator, into the refrigerator, close the refrigerator, haha, the program in the following way:

650 "this.width=650;" src= " Https://s3.51cto.com/wyfs02/M02/07/CC/wKiom1nQYj_yQ3kIAAASL3LCu4Q033.png-wh_500x0-wm_3-wmp_4-s_2073088506.png " title= "Picture 1.png" width= "179" height= "" border= "0" hspace= "0" vspace= "0" style= "width:179px;height:30px;" alt= " Wkiom1nqyj_yq3kiaaasl3lcu4q033.png-wh_50 "/> Later with f manipulate files. Close file: 650) this.width=650; "Src=" Https://s5.51cto.com/wyfs02/M01/A6/7E/wKioL1nQYhWAND1_AAANwKsY7tI721.png-wh_ 500x0-wm_3-wmp_4-s_3971033482.png "title=" image 2.png "width=" 146 "height=" "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:146px;height:30px; "alt=" wkiol1nqyhwand1_aaanwksy7ti721.png-wh_50 "/>

A in python represents append, equivalent to the >> in Linux

RB WB AB represents both binary files txt py text files without adding b.

r+ can, but the file does not exist it will not do any processing.

w+ can read and write, if the file does not exist to create, if it is overwritten.

A + can be read and written, if the file is not created and appended at the end.

F.read (1): One value per read f , if f.read () does not add parameters, then once read, re-read is ""

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M01/07/CC/wKiom1nQYn2jXYp0AABx0vHnETw167.png-wh_500x0-wm_ 3-wmp_4-s_3247193296.png "title=" image 3.png "width=" 257 "height=" "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width : 257px;height:150px; "alt=" wkiom1nqyn2jxyp0aabx0vhnetw167.png-wh_50 "/> \ n is a newline, accounting for one character.

Create a file and write

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M02/07/CC/wKiom1nQYpqwA2ifAAA1xKQAOsk300.png-wh_500x0-wm_ 3-wmp_4-s_4248377439.png "title=" image 4.png "width=" 338 "height=" "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width : 338px;height:150px; "alt=" Wkiom1nqypqwa2ifaaa1xkqaosk300.png-wh_50 "/>

Open by default with R , when only need to read the time can not add r

[email protected] ~]# cat du.py

f = open ("A.txt")

b = F.read ()

Print (b)---- here is the contents of the call to read and cannot be written directly to print B

F.close

[Email protected] ~]#

File copy process and case:

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M02/A6/7E/wKioL1nQYn2gSuR8AAEU8wxkGqA515.png-wh_500x0-wm_ 3-wmp_4-s_1519350735.png "title=" image 5.png "width=" "height=" "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width : 316px;height:200px; "alt=" Wkiol1nqyn2gsur8aaeu8wxkgqa515.png-wh_50 "/>

when a file is copied, it is automatically added with a name and the word "duplicate"

650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M01/A6/7E/wKioL1nQYp_QIUShAAIfYJ3aKao704.png-wh_500x0-wm_ 3-wmp_4-s_65219368.png "title=" Picture 6.png "width=" 368 "height=" "border=" 0 "hspace=" 0 "vspace=" 0 "style=" WIDTH:368PX; height:200px; "alt=" Wkiol1nqyp_qiushaaifyj3akao704.png-wh_50 "/>

ReadLine read Only one line, read the result is a string,readlines Read all the lines, read the result is in the list.

How to read large files:

ReadLine It's not a big line, and it's possible that all content without a newline character on one line cannot be used readline readlines read and while loop, then through read 0 when you exit, the case is as follows:

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M00/A6/7E/wKioL1nQYr-jqfszAABF35j8mJ8116.png-wh_500x0-wm_ 3-wmp_4-s_1824132466.png "title=" Picture 7.png "width=" 346 "height=" "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width : 346px;height:160px; "alt=" Wkiol1nqyr-jqfszaabf35j8mj8116.png-wh_50 "/>

positioning read/write:F.read ()represents theFread at the beginning of the file and use the pointer if you want to start reading from somewhere elseSeek(,x),asx=0represents the beginning of a file,x=1represents the position at this time,x=2represents the end of a file. F.seek (2,0)represents a two-character move from the beginning to the right. Read the contents of the file to be usedSeek(0,0) Pull it back,Python3negative movement is not supported, otherwise the file needs to be closed and reopened.

F.tell: To get the file where the seek pointer is, as follows:

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M01/07/CC/wKiom1nQYzPyJKFcAABmFRwr7Ew458.png-wh_500x0-wm_ 3-wmp_4-s_1251696869.png "title=" image 8.png "alt=" Wkiom1nqyzpyjkfcaabmfrwr7ew458.png-wh_50 "/>

common actions for files: by importing import OS, then os.rename ("a.txt"," B.txt") renamed a.txt to b.txt.

Common actions for folders: os.mkdir("Weipeng"), create a directory folder named Weipeng.

OS.GETCWD (): Gets the current directory structure, which is also the default directory, like pwdin linux , of course in py The path can also be modified by means of the open () file as follows:

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M02/A6/7E/wKioL1nQYwyyOfOUAAAQzcCMhj4489.png-wh_500x0-wm_ 3-wmp_4-s_1830205901.png "title=" image 9.png "width=" 263 "height=" "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:263px ; height:20px; "alt=" Wkiol1nqywyyofouaaaqzccmhj4489.png-wh_50 "/>

To change the directory creation file:

650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M00/07/CC/wKiom1nQY2fTUf5GAAAM-EQynpE229.png-wh_500x0-wm_ 3-wmp_4-s_4037990356.png "title=" image 10.png "width=" 211 "height=" "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width : 211px;height:120px; "alt=" Wkiom1nqy2ftuf5gaaam-eqynpe229.png-wh_50 "/>


It is important to get all the files under the current path:

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M02/A6/7E/wKioL1nQY0WDDEc7AADGUlaFKnc550.png-wh_500x0-wm_ 3-wmp_4-s_942172095.png "title=" image 11.png "width=" 255 "height=" "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width : 255px;height:300px; "alt=" Wkiol1nqy0wddec7aadgulafknc550.png-wh_50 "/>

Batch rename:

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M00/A6/7E/wKioL1nQY12A7dj3AAEhrJdbluQ267.png-wh_500x0-wm_ 3-wmp_4-s_2496473372.png "title=" image 12.png "width=" 346 "height=" $ "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width : 346px;height:200px; "alt=" Wkiol1nqy12a7dj3aaehrjdbluq267.png-wh_50 "/>


Working with files in Python

Related Article

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.