Python Fundamentals 10—I/O Programming

1. Input and output

  ①Input: input()

  ②Output: print()

1 name=input( ' Please enter the name ' )
 2  print (name)

2. File operation

  ①Open file: file object = open (file name, access mode, buffering)

    File name: Specify the file to be opened, usually including the path, which can be an absolute path or a relative path

    buffering: optional parameter, specifying the buffering method used by the file, buffering=0, no buffering; buffering=1, buffering one line; buffering>1, use the given value as the buffer size

    Access mode: used to specify the mode of opening the file, the parameter table of the access mode is as follows

Possible value meaning
r open for reading
w Open for writing, at this time the file content will be emptied before, if the file does not exist, the file will be created
a Open in append mode, starting at the end of the file, creating a new file if necessary
r+、w+ Open in read-write mode
a+ Additional read-write mode is on
rb Open in binary read mode
wb Open in binary write mode
rb + 、 wb + 、 ab + Open in binary read-write mode

  ②Close the file: file object.close()

  ③Read the file:

    Ⅰ: str=file object.read([b])

    Optional parameter [b]: specify the number of bytes to read, read all by default

    Ⅱ: list=file object. The content read by readlines() is returned to the string list

    Ⅲ: str=file object.readline() reads all lines in the file at once

    Ⅳ: Use the in keyword: for line in File object: process line data line

 1 f=open('test.txt')
 2 mystr=f.read()
 3 f.close()
 4 print(mystr)
 5 
 6 f=open('test.txt')
 7 while True:
 8     chunk=f.read(10)
 9     if not chunk:
10         break
11     print(chunk)
12 f.close
 1 f=open('test.txt')
 2 mylist=f.readlines()
 3 print(mylist)
 4 f.close()
 5 f=open('test.txt')
 6 while True:
 7     chunk=f.readline()
 8     if not chunk:
 9         break
10     print(chunk)
11 f.close()

3. Write the file

  ①write(): file object.write (written content)

  ②Additional writing: When opening a file, you can call open() with a or a+ as a parameter and then write

  ③writelines(): file object. (seq) is used to write a sequence of strings, seq is a sequence of returned strings (lists, tuples, sets, etc.)

1 f=open('test.txt','a+')
2 f.write('hello')
3 f.close()
4 f=open('test.txt')
5 for line in f:
6     print(line)
7 f.close()
1 mylist=['hello Jack','hello Salar','hello coffee']
2 f=open('test.txt','a+')
3 f.writelines(mylist)
4 f.close()
5 f=open('test.txt')
6 for line in f:
7     print(line)
8 f.close()

Fourth, the file pointer

  ①Get the position of the file pointer: pos=file object.tell() Returns an integer that represents the position of the file pointer. When opening a file, the position of the file pointer is 0. When reading and writing a file, the position of the file pointer will move to read write location

  ② Move the file pointer: file object .seek ((offset, where))  

    offset: the offset of the move, in bytes, move to the end of the file when it is positive, and move to the head of the file when it is negative

    where: refers to where to start moving, 0 is the starting position, when it is equal to 1, it is the current position to move, and when it is equal to 2, it starts to move from the end position  

1 f=open('test.txt')
2 print(f.tell())
3 f.close()
4 f=open('test.txt')
5 f.seek(5,0)
6 print(f.tell())
7 f.close()

5. Truncate the file

  File object. truncate([size]) Truncates the file from the beginning of the file, size is an optional parameter, the file content after size bytes will be truncated and discarded

1 f=open('test.txt','a+')
2 f.truncate(5)
3 f.close()
4 f=open('test.txt')
5 for line in f:
6     print(line)
7 f.close()

6. Other operations

  ①File attributes: Use the stat() function of the os module to obtain the attributes of the file, such as creation time, modification time, access time, file size and other attributes (import the module before use: import os)

  ②Copy files: Just use the copy() function of the shutil module. The function prototype is: copy(src, dst) means copying the source file src to dst

  ③Move the file: use the move() function of the shutil module, the function prototype is: move(src, dst)

  ④Rename the file: use the rename() function of the os module. The function prototype is: os.rename (original file name, new file name)

  ⑤ Delete files: use the remove() function of the os module, the function prototype is: os.remove(src)

1 import os
2 import shutil
3 fileStats=os.stat('test.txt')
4 print(fileStats)
5 
6 shutil.copy('F:\\python\\book\\ch7\\test.txt','F:\\python\\book\\ch7\\test2.txt')
7 shutil.move('F:\\python\\book\\ch7\\test2.txt','F:\\python\\book\\test2.txt')
8 os.rename('F:\\python\\book\\test2.txt','F:\\python\\book\\test3.txt')
9 os.remove('F:\\python\\book\\test3.txt')

Seven, directory programming

  ①Get the current directory: the getcwd() function of the os module

  ②Get directory content: listdir() function of os module

  ③Create a directory: the mkdir() function of the os module

  ④ Delete directory: rmdir() function of os module

1  import os
 2  print (os.getcwd())
 3  print (os.listdir(os.getcwd()))
 4 os.mkdir( ' F:\\python\\book\ch7\\he ' )
 5 os. rmdir( ' F:\\python\\book\ch7\\he ' )

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324978912&siteId=291194637