4. File Operations and file operations

Source: Internet
Author: User

4. File Operations and file operations
Computer systems include hardware, operating systems, and application hardware. The purpose is to run commands issued by software. The Hardware includes CPU, memory, and hard disk. Most CPUs have two modes: Kernel Mode and user mode. ① When the cpu is in the kernel state, the operating system is running and the hardware can be controlled (the instruction sets of all CPUs can be obtained) ② when the cpu is in the user State, the user software is running, hardware cannot be controlled (one subset of all cpu instruction sets can be obtained, which does not include the instruction sets for operating hardware) 4.1 Operating file process:

#1. to open a file, you have to get the file sentence and assign a variable f=open('a.txt ', 'R', encoding = 'utf-8') # The default open mode is r #2. data = f. read () #3. close file f. close () # This step is critical and can help save resources to close files. Note: Opening a file contains two resources: files opened at the operating system level + Application variables. After a file is operated, the two resources of the file must not be recycled. The recovery methods are as follows: 1. f. close () # reclaim files opened at the operating system level 2. del f # reclaim application-level variables where del f must occur in f. after close (), otherwise, the files opened by the operating system are not closed, and resources are occupied in vain. The python automatic garbage collection mechanism determines that we do not need to consider del f, this requires us to remember f. close () Although I said so, many people will still forget f. close (), we recommend that you use the with keyword to manage the context with open('a.txt ', 'w') as f: passwith open('a.txt ', 'R') as read_f,open(' B .txt ', 'w') as write_f: data = read_f.read () write_f.write (data)
4.2 file encoding
F = open (...) is opened by the operating system. If we do not specify the encoding for open, the default encoding for opening the file is obviously determined by the operating system,
The operating system uses its own default encoding to open the file, which is gbk in windows and UTF-8 in linux. To ensure that the file is not garbled and stored in any way, you must open the file in any way. F=open('a.txt ', 'R', encoding = 'utf-8 ')
4.3 open mode of the file
#1. the file opening mode is available (the default mode is text): r, read-only mode [default mode, the file must exist, and an exception is thrown if the file does not exist] w, write-only mode [unreadable; if the content does not exist, it is created. If the content exists, the content is cleared. If the content exists, only the write mode is appended. If the content does not exist, the content is created. If the content exists, only the content is appended. for non-text files, we can only use the B mode. "B" indicates that operations are performed in bytes (and all files are stored in bytes, in this mode, you do not need to consider the character encoding of text files, the jgp format of image files, and the avi format of video files.) rbwbab Note: When opened in the B mode, the read content is of the byte type, when writing data, you also need to provide the byte type. Encoding #3, '+' mode cannot be specified (that is, a function is added) r +, read and write [readable, writable] w +, write and read [writeable and readable] a +, write and read [writeable and readable] #4, read and write operations in bytes type, write and read modes r + B, read and write [readable, writeable: w + B; writeable and readable: a + B; writeable and readable]
4.4 File Operations 4.4.1 Common Operations

Read (3 ):

1. when the file is opened in text mode, it indicates that three characters are read. when the file is opened in B mode, it indicates that the cursor movement in the remaining three bytes is measured in bytes, for example, seek, tell, and truncate. Note: 1. seek has three ways of moving: 1, 2, where 1 and 2 must be in B mode, but either mode is 2 in bytes. truncate truncates a file. Therefore, the file must be writable, but cannot be opened by w or w +, because the file is cleared directly, therefore, truncate should test the effect in r +, a, or a + mode. The data of the 4.5 File Modification file is stored on the hard disk, so there is only overwriting, there is no such change, we usually see the modified file, are simulated results, specifically, there are two implementation methods: Method 1: load all the content of the file stored on the hard disk to the memory, which can be modified in the memory. After modification, then, the memory overwrites the hard disk (word, vim, nodpad ++, and Other Editors)
Import OS # Call the system module with open('a.txt ') as read_f,open('.a.txt. swap ', 'w') as write_f: data = read_f.read () # Read all data into the memory. replace ('Alex ', 'SB') # modify write_f.write (data) into memory and write the new file OS .remove('a.txt ') into the new file. # rename the new file to the original file.

Method 2: Read the content of the file stored on the hard disk One by one into the memory, write the content to the new file after modification, and overwrite the source file with the new file.

import oswith open('a.txt') as read_f,open('.a.txt.swap','w') as write_f:for line in read_f:line=line.replace('alex','SB')write_f.write(line)os.remove('a.txt')os.rename('.a.txt.swap','a.txt')

Related Article

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.