python-Basics-File operations

Source: Internet
Author: User

1 opening and closing of files

Open it

In Python, using the Open function, you can open a file that already exists, or create a new file

Open (file name, access mode)

Examples are as follows:

    f = open(‘test.txt‘, ‘w‘)

Description

access Mode Description
R Open the file as read-only. The pointer to the file will be placed at the beginning of the file. This is the default mode.
W

Open a file for writing only.

Overwrite the file if it already exists. If the file does not exist, create a new file.

A

Opens a file for appending. If the file already exists, the file pointer will be placed at the end of the file.

In other words, the new content will be written to the existing content. If the file does not exist, create a new file to write to.

Rb Opens a file in binary format for read-only. The file pointer will be placed at the beginning of the file. This is the default mode.
Wb

Open a file in binary format only for writing.

Overwrite the file if it already exists. If the file does not exist, create a new file.

Ab

Opens a file in binary format for appending.

If the file already exists, the file pointer will be placed at the end of the file.

In other words, the new content will be written to the existing content. If the file does not exist, create a new file to write to.

r+ Open a file for read-write. The file pointer will be placed at the beginning of the file.
w+

Open a file for read-write.

Overwrite the file if it already exists. If the file does not exist, create a new file.

A +

Open a file for read-write. If the file already exists, the file pointer will be placed at the end of the file.

The file opens with an append mode. If the file does not exist, create a new file to read and write.

rb+ Opens a file in binary format for read-write. The file pointer will be placed at the beginning of the file.
wb+

Opens a file in binary format for read-write.

Overwrite the file if it already exists. If the file does not exist, create a new file.

ab+

Opens a file in binary format for appending. If the file already exists,

The file pointer will be placed at the end of the file. If the file does not exist, create a new file to read and write.

Close File

Close ()

Examples are as follows:

# Create a new file with the file name:    test.txt = Open ('test.txt'w')    # Close this file    f.close ()

2 reading and writing of files

3 Locating and reading files

<1> get the current read and write location

In the process of reading and writing files, if you want to know the current location, you can use tell () to get

# Open a file that already exists F= Open ("Test.txt","R") Str= F.read (3) Print"The data read is:", str # find current location position=F.tell () print"Current file location:", Position str= F.read (3) Print"The data read is:", str # find current location position=F.tell () print"Current file location:", Position f.close ()

<2> Navigate to a location

If you need to operate from another location while reading and writing files, you can use Seek ()

Seek (offset, from) has 2 parameters

    • Offset: Offsets
    • From: direction
      • 0: Indicates the beginning of the file
      • 1: Indicates the current position
      • 2: Indicates end of file

Demo: Set the location to: from the beginning of the file, offset by 5 bytes

# Open a file that already exists F= Open ("Test.txt","R") Str= F.read ( -) Print"The data read is:", str # find current location position=F.tell () print"Current file location:", Position # reset position F.seek (5,0) # Find the current location position=F.tell () print"Current file location:", Position f.close ()

Demo: Set the location to: from the end of the file, at 3 bytes

# Open a file that already exists F= Open ("Test.txt","R") # Find the current location position=F.tell () print"Current file location:", Position # reset position F.seek (-3,2# The data read is: File last 3 bytes data str=f.read () print"The data read is:", str f.close ()
4 renaming and deletion of files

Sometimes, you need to rename files, delete, and so on, Python's OS module has such a function

<1> file renaming

Rename () in the OS module can complete renaming of files

Rename (need to modify the file name, the new file name)

    import os    os.rename("毕业论文.txt", "毕业论文-最终版.txt")

<2> Delete Files

Remove () in the OS module can complete the deletion of the file

Remove (file name to be deleted)

    import os    os.remove("毕业论文.txt")
Related Actions for folders

In actual development, it is sometimes necessary to use the program to do certain things, such as creating, deleting, etc.

Just as the OS module is required for file operations, the OS module is also required if you want to manipulate folders

<1> Create a folder

    import os    os.mkdir("张三")

<2> Get current directory

    import os    os.getcwd()

<3> Change the default directory

    import os    os.chdir("../")

<4> Get Catalog List

    import os    os.listdir("./")

<5> Delete a folder

    import os    os.rmdir("张三")

python-Basics-File operations

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.