python--anonymous functions, file operations

Source: Internet
Author: User

Tag: value read-only down position close import FSE Direction Listdir

First, anonymous function
    • Grammar:
    sum=lambda+ arg2    #调用sum函数    print"Value of total : "sum1020 )    #结果为:30    print"Value of total : "sum2020 )    #结果为:40
  • Application situations

      • As an argument to a function
    def fun(a, b, opt):    print("a =", a)    print("b =", b)    print("result =", opt(a, b)fun(12lambda+ y     #结果为:a = 1 b = 2 result = 3
      • As a parameter to the Python built-in function.
    = [    {"name":"zhangsan""age":18},     {"name":"lisi""age":19},     {"name":"wangwu""age":17}]#将以上列表按姓名排序。=lambda x:x[‘name‘])
Ii. Documents
  • Open File:

      • Use the open () function.
    file=open(‘test.txt‘‘r‘)
    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.
  • To close a file:

    • Use the close () function
    file.close()
  • Read file:

    • Use the Read () function
    file.read()file.read(1)    #每次只读一个字节file.read(2)    #每次读两个字节
    • Use the ReadLines () function: ReadLines can read the contents of the entire file as a row and return a list, where the data for each row is an element

    • Use the ReadLine () function: Read only one row in a file

  • Write content:

Use the Write () function

python file.write(‘12345‘)

    • Positioning read/write:
      • Gets the location of the current file read: Tell ()
      • Navigate to a location: Seek (offset, from)
        • Offset: Offsets
        • From: direction
          • 0: Indicates the beginning of the file
          • 1: Indicates the current position
          • 2: Indicates end of file
    • File Rename: rename ():
import osos.rename(‘test.txt‘‘1.txt‘)os.remove(‘1.txt‘)os.mkdir(‘test‘)os.rmdir(‘test‘)os.getcwd()os.chdir(‘../‘)os.listdir(‘./‘)
    • Delete file: Remove ()

    • Create folder: mkdir ()
    • Delete folder: RmDir ()
    • Get current directory: GETCWD ()
    • Change default directory: ChDir ()
    • Get directory list: Listdir ()

python--anonymous functions, 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.