Python-- from scratch to learn object-oriented development 5

Python-- from scratch to learn object-oriented development 5

Taiyuan University of Technology Robotics Team 20 punch card day13

file

1. The concept and role of documents

  • The computer file is stored in some long-term storage facility for some of the data
  • Long-term storage devices include: hard disk, U disk, removable hard disk, CD-ROM

The role of file

The long-term data storage, use when needed

2. file storage

  • In the computer, the file is a binary way to save on disk

Text and binary files

  • Text File
    • You can use a text editor to view
      on the nature or binary files
      such as: python source code
  • binary file
    • Save the content is not giving a direct reading, but available to other software used
    • For example: image files, audio files, video files, etc.
    • Binary files can not use a text editor to view

The basic file operations

2.1 routine file operations

Routine operation of the file is to be fixed in the computer, comprising a total of three steps:

  1. open a file
  2. Reading and writing files
    • Read the contents of the file is read into memory
    • Write memory contents written to the file
  3. Close the file

2.2 file operation function / method

Function / method Explanation
open Open the file, and returns the object file operations
read The contents of the file is read into memory
write Write the contents of the specified file
close Close the file
  • open function is responsible for opening the file and returns the file object
  • read / write / close through three methods require a file object to call

2.3read method - read the file

  • The first argument to the open function is to open the file name (file name is case-sensitive)
    • If the file exists , it returns the file operation object
    • If the file does not exist , it will throw an exception
  • read method can be read once and return all the contents of the file
  • responsible for close method to close the file
    • If you forget to close the file will cause the system resource consumption, and will affect subsequent access to the file
  • Note : After performing read method, will the file pointer is moved to the end of the file
# 1. 打开 - 文件名需要注意大小写
file = open("README")

# 2. 读取
text = file.read()
print(text)

# 3. 关闭
file.close()

prompt

  • In development, often are written to open and close the code, then write intermediate files for read / write operations!

File pointer

  • File pointer mark start position from which to read data
  • The first time you open a file, usually beginning of the file pointer to the file
  • When performing a read method, the file pointer is moved to the end of the read content
    • By default, it will move to the end of file

2.4 Open file

  • The default function to open read-only open the file, and returns the file object

The syntax

f = open("文件名", "访问方式")

Here Insert Picture Description
prompt

  • Frequent move the file pointer, will affect the efficiency of reading and writing files , more development time will be read-only, write-only way to manipulate files

Examples of written documents

# 打开文件
f = open("README", "w")

f.write("hello python!\n")
f.write("今天天气真好")

# 关闭文件
f.close()

2.5 rows to read the file contents

  • The default method will read the file of all the contents of a one-time read into memory
  • If the file is too large, memory usage will be very serious

readline method

  • readline method reads a single line
  • After performing the method, will the file pointer to the next line, ready to be read again

Correct posture to read large files

# 打开文件
file = open("README")

while True:
	# 读取一行内容
	text = file.readline()

	# 判断是否读到内容
	if not text:
	break

	# 每读取一行的末尾已经有了一个 `\n`
	print(text, end="")

# 关闭文件
file.close()

Common management operations file / directory

  • In the terminal / file browser , you can perform regular file / directory management operations, such as:
    • Create, rename, delete, change the path to view the contents of the directory, ......
  • In Python, if you want to achieve the above functions by a program, you need to import the module os

File Operations

Method name Explanation Examples
rename Rename the file os.rename (source file name, the target file name)
remove Delete Files os.remove (filename)

Directory operations
Here Insert Picture Description
to date, I will update the basics of python over, which is not included in reptiles, interface design. Because I am the main direction is the direction robots, rather than program development. So the next step would be for my updated based on the opencv python. Analysis of robot vision system is a very important part ...

If you are interested please pay attention to tomorrow.

Published 14 original articles · won praise 66 · views 20000 +

Guess you like

Origin blog.csdn.net/soul_study/article/details/104811307