Give you a file "Contact .txt", how to view the content?
1. Install a text editor software
2. Use a text editor to open the software
3. Review or write
4. Save and close
File operations are divided into read, write, modify, we start to start learning to read
Open = F (File = " E: / Lu Jia / python / Information .txt " , MODE = ' R & lt ' , encoding = ' GBK ' ) Data = reached, f.read () f.close
File - binary mode (video, photos, text transmission network with rb mode)
Open = F (File = " E: / Lu Jia / python / Information .txt " , MODE = ' RB ' ) Data = reached, f.read () f.close Print (Data)
File Processing - Intelligent detection coding tools
Q: The difference between Example 2 and Example 1 in where?
A: When encoding is not specified Example 2 opens the file, this is why? Because to directly open the file mode rb, rb refers to binary mode, data is directly read memory bytes format, if want to see content, also need to manually decode, thus opening stage in the document, was no need for coding
Q: If you do not know the file you want to deal with what is coded how to do it?
First, install third-party toolkit chardet
E: \ Lu Jia \ python> pip install chardet
Import the chardet F = Open (File = " E: / Lu Jia / python / Information .txt " , MODE = ' RB ' ) Data = reached, f.read () f.close Result = chardet.detect (Data) Print (Result )
Circular file:
f = open('联系方式.txt',mode = 'r', encoding = 'gbk') for line in f: print(line) f.close()
The results :( Note: empty lines between the results, print will automatically add a blank line)
Zhang Yao 18456898767
Fang 13,569,876,898
Yang Chao 11345566778
File - File write mode, w is not modified to create a pattern, if already on file with the w, will clear the original content
Open = F ( ' Information .txt ' , MODE = ' W ' , encoding = ' GBK ' ) f.write ( ' Star \ t18909890988 ' ) f.close ()
File - file append mode of operation (the contents appended to the end of the file)
Open = F ( ' Information .txt ' , MODE = ' A ' , encoding = ' GBK ' ) f.write ( ' \ n-east \ t18789007657 \ n- ' ) f.write ( ' day \ t18982564562 \ n- ' ) f.write ( ' Yang Ying \ t13658479875 \ the n- ' ) f.close ()
note:
- When the file operation, to 'a' or 'ab' mode is on, the only additional, namely: the additional content at the end of the original content
- When writing to the hard disk, you must be some kind of coded 0101010, need to pay attention when open:
- ab &, need to be passed directly to that encoding 0101010, i.e., writing byte type
- and a encoding, encoding need to pass the string is written, based on the internal encoding unicode string code developed for converting binary coded form
File - File mixing operation
Mixed mode write
Open = F ( ' Information .txt ' , MODE = ' R & lt + ' , encoding = ' GBK ' ) Data = reached, f.read () Print (Data) f.write ( ' clouds \ t18789007657 \ n- ' ) f.write ( ' Li \ t18982564562 \ n- ' ) f.write ( ' Young light \ t13658479875 \ n- ' ) f.close ()
File - File to another function
DEF fileno (Self, * args, ** kwargs): # Returns the file handle index value in the kernel, it can be used when doing IO multiplexing later DEF flush (Self, * args, ** kwargs): # the files from memory buffer in force a refresh to the hard disk DEF readable (Self, * args, ** kwargs): # determine whether readable DEF readline (Self, * args, ** kwargs): # read only one line, encountered \ r or \ n far DEF seek (Self, args *, ** kwargs): # the operation of the cursor position to the specified file # note seek length is calculated in bytes, the byte length character encoding memory occupied by each character of not the same as # The "flying path studies" as used gbk 2 bytes stored a word, utf-8 with 3 bytes is DEF Seekable (Self, args *, ** kwargs): # determines whether the file can be seek operating defTell (Self, args *, ** kwargs): # Returns the current cursor position file operation DEF TRUNCATE (Self, args *, ** kwargs): # specified truncate the file, the specified length, then it is truncated from the beginning of the specified file length, the length is not specified, then it is from the current position to the end of the file contents of all removed DEF Writable (Self, * args, ** kwargs): # determine whether the file is writable
File - File editing
First, modify the original papers
1 def alter(file,old_str,new_str): 2 """ 3 替换文件中的字符串 4 :param file:文件名 5 :param old_str:就字符串 6 :param new_str:新字符串 7 :return: 8 """ 9 file_data = "" 10 with open(file, "r", encoding="utf-8") as f: 11 for line in f: 12 if old_str in line: 13 line = line.replace(old_str,new_str) 14 file_data += line 15 with open(file,"w",encoding="utf-8") as f: 16 f.write(file_data) 17 18 alter("file1", "09876", "python")
Second, the original contents of the file and write the way you want to modify the contents of the new file storing
2.1 python string replacement method, change the contents
os Import DEF the ALTER (File, old_str, new_str): "" " will replace the string to a new file, and then delete the original file, the new file name instead of the original file : param file: File Path : param old_str: string needs to be replaced : param new_str: replace the string : return: None "" " with Open (File," R & lt ", encoding =" UTF-. 8 ") AS F1, Open ("% s.bak " File%, "W", encoding = "UTF-. 8") AS F2: for Line in F1: IF old_str in Line: Line = line.replace (old_str, new_str) f2.write (Line) The os.remove (File) os.rename ( "% s.bak"% File, File) the ALTER ( "file1", "Python", "test")
2.2 python using regular expressions to replace the file content replacement method re.sub
1 import re,os 2 def alter(file,old_str,new_str): 3 4 with open(file, "r", encoding="utf-8") as f1,open("%s.bak" % file, "w", encoding="utf-8") as f2: 5 for line in f1: 6 f2.write(re.sub(old_str,new_str,line)) 7 os.remove(file) 8 os.rename("%s.bak" % file, file) 9 alter("file1", "admin", "password")