"Programmer Technology Practice" learning a scripting language Python (i) file processing

Source: Internet
Author: User

Now the main language of work is Java,java in enterprise-class applications can play a very good use, but sometimes to do a small function, such as batch update files, crawl pages, and so on, this time with Java is too cumbersome. So I learned the script language of Python.

This essay is mainly about how Python handles text files and consolidates some of the basics of Python.

All right, let's go.

Demand Elaboration
    • Working with text files: read a local file, row by line

When it comes to processing files, we have to say Python's built-in function open

Open (file, mode= ' R ', buffering=-1, encoding=none, errors=none, newline=none, closefd=true, opener=none)

function definition: Open a file and return the corresponding file object, if the specified file cannot be opened, then oserror will throw

Where the file parameter represents the name of the file that needs to be opened,

modeis open mode,

buffering is used to control the buffer of the file, the default value is 0 , is not buffered, set to 1 have a buffer,

encoding is the file encoding, the default is System encoding,

Errors is an optional parameter that specifies how the file will be handled when an error occurs during encoding and decoding, and note that this error handling is not applied to mode (' B ') in binary

NewLine The working mode used to control universal line breaks (only works in text mode)

modePatterns can be in the following ways:

character meaning
' R ' Open as read-only (default)
' W ' Open in write mode, first delete the contents of the file
' X ' Opened exclusively when a file already exists
A Open as write, add content at the end of the content when the file is present
' B ' Binary
' t ' Text mode (default)
+ Update a disk file (reading and writing)
U Universal newlines mode (deprecated)

In general, you can get the contents of a file by simply pressing the file open() name parameter instead of adding any other parameters when using the function. The default mode is read mode, and if you read special files (such as video or image files), you must use the ' B ' mode.

Here is an example of working with text:

1 #Encoding=utf-82File_path ="D:/test.txt"3File =Open (File_path)4 5 ## # # #1. Read () #####6 7 Print(File.read ())8 9 ## # # #2. ReadLine () #####Ten  Oneline =File.readline () A  whileLine : -     Print(line) -line =File.readline () the  - ## # # #2. ReadLines () ##### -  -  forLineinchfile.readlines (): +     Print(line)

File.close ()

Listed above are 3 ways to read file: read (), ReadLine (), ReadLines ()

    • readlines: The most common way is to convert each line of the file into a list, and then you can do all kinds of things .
    • Read: Represents the full contents of a one-time return file
    • ReadLine: Returns a row of the file, and continues to read the next line on the next call until it finishes reading

OK, here's an example of processing the file!

"Programmer Technology Practice" learning a scripting language Python (i) file processing

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.