Management of python-files

Source: Internet
Author: User

Input and output of characters
Input (raw_input/input)---> program processing---> Output (print)

I. Three major steps in document management:


1. Open the file; f = open ("filename")
2. Processing of documents;
F.read () can read file contents
F.write ("Hello") cannot write to the file because the default file is opened in R mode;
3. Close and save the file; F.close ()

The file must be closed after it is used because the file object consumes the resources of the operating system.


Two. Summary of several models of the document:

1. R mode:
If the file does not exist, direct error;
The file can only be read and cannot be written;

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M01/06/04/wKiom1mv7hnivYWdAAAvW2ure_I367.png "style=" float : none; "title=" screenshot from 2017-09-06 20-43-53.png "alt=" Wkiom1mv7hnivywdaaavw2ure_i367.png "/>

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M00/A4/B5/wKioL1mv7fvSe4G-AACIUIkeqh4749.png "style=" float : none; "title=" screenshot from 2017-09-06 20-45-22.png "alt=" Wkiol1mv7fvse4g-aaciuikeqh4749.png "/>


2. W Mode:
If the file does not exist, create the file directly;
The file can only be written and cannot be read;
The contents of the original file will be emptied when the file is opened;

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M00/A4/B5/wKioL1mv7m6hHZj6AACDw-C9OaQ238.png "title=" Screenshot from 2017-09-06 20-47-26.png "alt=" Wkiol1mv7m6hhzj6aacdw-c9oaq238.png "/>


3. r+ Mode:
If the file does not exist, direct error;
Documents can be read or written;
Where the content of the specific write is determined by the file pointer (handle);

Read and write first

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/A4/B5/wKioL1mv76OD8VXwAABjbSOV9ZM098.png "style=" float : none; "title=" screenshot from 2017-09-06 20-51-46.png "alt=" Wkiol1mv76od8vxwaabjbsov9zm098.png "/>


Write first and then read

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/06/04/wKiom1mv78KQnKN-AABOfuFFGpw164.png "style=" float : none; "title=" screenshot from 2017-09-06 20-52-29.png "alt=" Wkiom1mv78kqnkn-aabofuffgpw164.png "/>


4. w+ Mode:
If the file does not exist, create the file directly;
Files can be written and read;
The contents of the original file will be emptied when the file is opened;

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M02/06/04/wKiom1mv8F2gHzuSAABfBbulno8009.png "title=" Screenshot from 2017-09-06 20-55-13.png "alt=" Wkiom1mv8f2ghzusaabfbbulno8009.png "/>

5. A
If the file does not exist, create the file directly;
The file can only be written and cannot be read;
The contents of the write are appended directly to the final file;

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/06/04/wKiom1mv8PKCxSPBAACUGAsZbno186.png "title=" Screenshot from 2017-09-06 20-57-32.png "alt=" Wkiom1mv8pkcxspbaacugaszbno186.png "/>


6. A +
If the file does not exist, create the file directly;
Files can be written and read;
The contents of the write are appended directly to the final file;

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/06/04/wKiom1mv8TCTNj0iAACpyUiO9ak089.png "title=" Screenshot from 2017-09-06 09-47-28.png "alt=" Wkiom1mv8tctnj0iaacpyuio9ak089.png "/>


7. b
Open the file in binary mode, can be used in combination with r,w,+ and so on;

binary files
To read a binary file, compare slices, videos, etc., open the file with ' RB ' mode
>>> f = open ('/root/test.jpg ', ' RB ')
>>> F.read ()
' \xff\xd8\xff\xe1\x00\x18exif\x00\x00 ' # Hexadecimal representation of bytes



Three. Read the file

Append the contents of the file in the passwd to the Hello.txt

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M00/A4/B5/wKioL1mv81jgTUzPAAAZOi95oZk804.png "style=" float : none; "title=" screenshot from 2017-09-06 10-04-54.png "alt=" Wkiol1mv81jgtuzpaaazoi95ozk804.png "/>

Read () reads all the contents of the file directly;

ReadLine () reads the file one line at a time, the return type is a string;

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M00/06/04/wKiom1mv83ehEPVSAABlhgQBpWE936.png "style=" float : none; "title=" screenshot from 2017-09-06 10-04-43.png "alt=" Wkiom1mv83ehepvsaablhgqbpwe936.png "/>


readlines () reads all the contents of the file and returns it as a list;

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M01/06/04/wKiom1mv83jSuDviAACWOzr6ogg286.png "style=" float : none; "title=" screenshot from 2017-09-06 10-05-33.png "alt=" Wkiom1mv83jsudviaacwozr6ogg286.png "/>


xreadlines () Similar generators , the contents of the file are not returned directly in a list;

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M02/A4/B5/wKioL1mv81nS3bO9AACc69rx4sI918.png "style=" float : none; "title=" screenshot from 2017-09-06 10-07-44.png "alt=" Wkiol1mv81ns3bo9aacc69rx4si918.png "/>

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M01/06/04/wKiom1mv83jgBZa-AADrrFxzu4k819.png "style=" float : none; "title=" screenshot from 2017-09-06 10-16-32.png "alt=" Wkiom1mv83jgbza-aadrrfxzu4k819.png "/>




Four. Writing the file
Write ("str") # writes a string to a file
Writelines ("[" ["Hello", "World"] ") # writes each element of the sequence to a file;

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M01/06/04/wKiom1mv9abQGS-3AAA6V5cqL0I170.png "style=" float : none; "title=" screenshot from 2017-09-06 21-16-09.png "alt=" Wkiom1mv9abqgs-3aaa6v5cql0i170.png "/>

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/A4/B6/wKioL1mv9YfxK-2EAABOoThEXmM238.png "style=" float : none; "title=" screenshot from 2017-09-06 21-17-40.png "alt=" Wkiol1mv9yfxk-2eaaboothexmm238.png "/>


Five. Other operations of the file

1.f.seek (offset, option)
The offset, if positive, represents the right offset, or, if negative, the left offset;
option, if 0, the pointer points to the beginning of the file;
If 1, the pointer points to the current position;
If 2, the pointer points to the end of the file;



2.f.flush () Submit a modification to the document;


Six. Several points of note in the documentation:


1. The file object is an iterative data type that can be used for traversing the contents of a file;

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M01/A4/B6/wKioL1mv9wnR9YMcAAA7z1mumGQ318.png "title=" Screenshot from 2017-09-06 21-24-02.png "alt=" Wkiol1mv9wnr9ymcaaa7z1mumgq318.png "/>


2.with open ("filename") as F: This method avoids the user forgetting to close the file; don't write F.close ()

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/06/05/wKiom1mv91qTFTsJAAAoThwWTPQ728.png "title=" Screenshot from 2017-09-06 21-25-01.png "alt=" Wkiom1mv91qtftsjaaaothwwtpq728.png "/>


Management of python-files

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.