"Python" Java Programmer learning Python (11)-io

Source: Internet
Author: User

  

First, preface

Io is still a lot of content, and now I just understand the preliminary usage, of course, the details should refer to the official API:

    • Official api:https://docs.python.org/3/library/os.html.
    • Chinese api:http://python.usyiyi.cn/translate/python_352/library/os.html.
Second, the basic operation of the file

The basic operation of the file can use the OS and SHUITL modules, which have basic functions to meet the general requirements.

    • Os.makedirs (): Create a folder that does not exist in the middle will also be created
    • Os.path.abspath (PATH): Returns the absolute path of the path, which can be used for relative absolute
    • Os.path.getsize (PATH): View the number of bytes in a file
    • Os.path.isfile (PATH): Determine if it exists and is a file
    • Os.path.isdir (PATH): Determine if it is present and is a folder
    • Os.path.exists (PATH): Determine if there is
    • Os.rename (Current_file_name, new_file_name): Renaming files
    • Os.remove (file_name): Deleting files
    • Shuitl.copy (SRC, DST): Copying files
    • Shuitl.copytree (SRC, DST): Copy Folder

See the API for more details.

Third, the basic operation of the content

First, you must be proficient in applying the global function open to get the file (clip) object, function definition:

File open (file, mode='r', Buffering=-1, Encoding=none, Errors=none, Newline=none, closefd= True, Opener=none)

Although there are many parameters, you can see that all but the path to the file have default values.

Mode

      • R: Open read-only, pointer in file header
      • W: Open Write, overwrite if file is present
      • A: Open write, append to end If file exists
      • B: Binary mode for combination with other modes such as RB, WB, AB, etc.
      • +: Open file for read/write

Buffering:

      • 0: Off buffering, only allowed in binary mode
      • 1: Select row buffer, only available in text mode

Encoding: Default encoding and platform-dependent

NewLine: Row split, default to line break

Once the file object is obtained, you can invoke the method to manipulate it.

    • File.read (): reads the specified number of bytes from the file, or reads all if not given or negative.
    • File.readline ([size]): reads the entire line
    • File.next (): reads the next line
    • File.readlines (): reads all rows into a list of strings, each of which is a single line of content (including newline characters)
    • File.write (str): Write content that does not automatically add line breaks
    • File.close (): Close Open File
    • File.tell (): Gets the position of the current pointer
    • File.seek (offset[, whence]): Sets the current location of the file
    • File.flush (): Flush buffer
    • File.truncate ([size]): Intercepts the file, intercepts the bytes specified by size, and defaults to the current file location.

Mastering these can be, of course, to see more documents as the main.

"Python" Java Programmer learning Python (11)-io

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.