Python-based file processing

Source: Internet
Author: User

I. Document processing process
    1. Open the file, get the file handle and assign a value to a variable
    2. Manipulating a file with a handle
    3. Close File
positive fruit Interesting fruit fruit: Guo Yan qu: Chen granule arrangement/Mix/Harmony: Yanchi: Words:guo 婞 song:chen tabletsarrange/Mix/Harmony:yan Chixiao:well You walnut sword He will be a trick don't face wow ah ah ah yes lose no money in no money you wear peach down Demon Swordhe will shamelesslywow Yeahlost inchThe absence of MoneylostinchThe absence of money are you willing to die without envy fairy mended gentle end of the temple of longevity Hey hey hey lost to good face to the good face you would like the notEnvy Centsmummy Gentle death of the empty Palacehey Xi xilost to good appearancelost to good appearance life 30,000 tianqu fruit a lonely I'm going to cook a bowl of noodles for you. Life isThirty thousand daysfun fruit there isno solution between Solitudehard practicing smiling half-Step Epilepsyi'll go and cook your bowlWith the snow in his heart, the volunteers are called the little pathetic. Sudden-like underage heart of the snow big Volunteerwas called a small Pitifulalassudden sample of Minorsudden Sample of minor This want pledged Oufei time only negative tail born about aye hush Hush really afraid of fire refining really fear fire the desire to set the wind blood MONTHSWU Flying The tail about Aye Boo booreally afraid's fire refiningreally afraid of fire alive 30,000 tianqu fruit There's a lonely, hard-boiled, smiling half-step, I'm going to make you a bowl of noodles life. isThirty thousand daysfun fruit there isno solution between Solitudehard practicing smiling half-Step Epilepsyi'll go and cook your bowlright and wrong 20 read ten views of the mediocre love que self-study sleep Arhat fist scare don't know jiangzi good danger right andwrong Twenty readsquare view over the Yong love Queself-study Sleep Lohan boxingscare know that a good risk of Jiang XI model file contents
Sample Document ContentTwo. Basic Operation 2.1 The basic process of file operation the file save code is as follows

The wrong way to open the moment

The right way to open
F=open (' Chenli.txt ', encoding= ' GBK ') f.read ()
2.3 File Open Mode
1 File Handle = open (' File path ', ' mode ')

When you open a file, you need to specify the file path and how you want to open the file, and then open it to get the file handle and manipulate it later through the file handle.

The mode of opening the file is:

    • R, read-only mode "default mode, file must exist, not present, throw exception"
    • W, write-only mode "unreadable; not exist" created; empty content "
    • X, write-only mode "unreadable; not present, create, present error"
    • A, append mode "readable; not present, create; append content only"

"+" means you can read and write a file at the same time

    • r+, read and write "readable, writable"
    • w+, write "readable, writable"
    • x+, write "readable, writable"
    • A +, write "readable, writable"

"B" means to operate in bytes

    • RB or R+b
    • WB or W+b
    • XB or W+b
    • AB or A+b

Note: When opened in B, the content read is a byte type, and a byte type is required for writing, and encoding cannot be specified

2.4 File built-in functions flush

Flush principle:

    1. File operation is the software to read files from the hard disk to memory
    2. The operation of writing to the file is also stored in buffer buffers (memory speed faster than the hard disk, if the data written to the file from memory to the hard disk, memory and hard disk speed delay will be infinitely amplified, inefficient, so to brush the data to the hard disk we unified into the memory of a small piece of space, buffer, After some time the operating system will flash the data in buffer to the hard disk.
    3. Flush that is, forcing the written data to be brushed to the hard disk

Scroll bar:

Import Sys,timefor I in  range:    sys.stdout.write (' # ')    Sys.stdout.flush ()    time.sleep (0.2)
2.5 cursor movement within the file

Note: Read (3) represents reading 3 characters, and the rest of the files within the cursor movement are in bytes such as Seek,tell,read,truncate

Finishing in

2.6 Open Function detailed

1. Open () syntax

Open (file[, mode[, buffering[, encoding[, errors[, newline[, Closefd=true] []] ])
The Open function has a number of parameters, commonly used file,mode and encoding
file files, need to be quoted
mode File open mode, See the following 3
buffering for 0,1,>1 three, 0 for buffer off (binary mode only), and 1 for line buffer (text mode only),> 1 indicates the buffer size of the initialization;
encoding indicates what encoding is used for the returned data, generally UTF8 or GBK;
Errors The value is generally strict,ignore, when taking strict, character encoding problems, will error, when taking ignore, coding problems, the program will be ignored, continue to execute the following program.
NewLine can take a value of None, \ n, \ r, ", ' \ r \ n ', to differentiate between newline characters, but this parameter is valid only for text mode;
closefd , is related to the file parameters passed in, by default, True, the file parameter passed to the file name, the value is false, file can only be a document descriptor, what is a file descriptor, is a non-negative integer, in the Unix kernel system, open a file, A file descriptor is returned.

2. the difference between file () and open () in Python
Both can open the file, the operation of the file, but also have similar usage and parameters, but, the two file open way there is an essential difference, filefor the document class , the file () to open files , equivalent to this is in the construction of the file class, and open () Opening the file is done using Python's built-in functions , and it is recommended to use the Open

3. Basic values of the parameter mode

Character meaning
' R ' open for reading (default)
' W ' open for writing, truncating the file first
' a ' open for writing, appending to the end of the file if it exists
' B ' binary mode
T ' text mode (default)
' + ' open a disk F Ile for updating (reading and writing)
' U ' Universal Newli NE mode (for backwards compatibility; Should is used in new code)

R, W, A is the basic mode of open file, corresponding to read-only, write-only, append mode;
B, T, +, u these four characters, with the above file open mode combination, binary mode, text mode, read and write mode, universal line break, according to the actual situation combination of use,

Common mode value Combinations

1 R or RT default mode, text mode read 2 RB   binary file 3      4 w or wt text mode write, open before file storage is emptied 5 WB  binary Write, file storage is also emptied 6      7 a  append mode, can only be written at the end of the file 8 a + readable Write mode, write can only be written at the end of the file 9     

  

  

  

  

Python-based file processing

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.