1. Write data
1 f = open( " hello.txt " , " w " ) 2 f.write( " hello world python! " ) 3 f.close() #Close the file is essential
If the file does not exist, it will be created, if it exists, the content in it will be emptied, and then the data will be written.
Second, read data
1 f = open( " hello.txt " , " r " ) 2 content = f.read() 3 4 print (content) 5 f.close() #Close the file is essential
To find a variable to store the read content, if no data is passed to the parentheses after read, the content of the entire file is read by default, which is consistent with the function of readlines(), use readlines()
What is returned is a list, each line of data is an element; in addition, readline() only reads one line of data at a time. If a value such as 1 is passed in, one byte of data is read. If used multiple times
Read data operation, then the data read later starts from the position after the last read.
3. Random reading
and writing of files 1. Use tell() to get the current reading and writing position
1 f = open( " Anonymous function.py " , " r " , encoding= ' UTF-8 ' ) #Add encoding='UTF-8' when the file contains Chinese 2 str = f.read(4 ) 3 4 print ( "The read data is: " + str) 5 6 position = f.tell() #Get the current read and write position 7 print (position) 8 9 str = f.read(4) #Then read 4 Bytes of data 10 print (str) 11 12 position = f.tell()#Get the current read and write position 13 print (position) 14 15 f.close()
2. Set to a certain position
If you need to operate from another position during the process of reading and writing files, you can use seek()
seek(offset,form) There are two parameters:
offset: offset
from: direction
0 : Indicates the beginning of the file
1: Indicates the current position
2: Indicates the end of the file
1 f = open( " Anonymous function.py " , " r " , encoding= ' UTF-8 ' ) #Add encoding='UTF-8' when the file contains Chinese 2 str = f.read(10 ) 3 4 print ( "The read data is: " + str) 5 6 #Find the current position 7 position = f.tell() 8 print (position) 9 10 #Reset position 11 f.seek(5 ,0) 12 13 # Find current location 14 position = f.tell() 15 print(position) 16 17 f.close()
Fourth, the related operations of the file
1. Rename
1 import os 2 3 os.rename( " The first version of the paper.txt " , " The final version of the paper.txt " )
2. Delete files
1 import os 2 3 os.remove( " The first edition of the paper.txt " )
Five, folder related operations
1. Create a folder
1 import os 2 3 os.mkdir("hello")
2. Get the current directory
1 import os 2 3 os.getcwd()
3. Change the default directory
1 import os 2 3 os.chdir("../")
4. Get the directory listing
1 import os 2 3 os.listdir("./")
5. Delete the folder
1 import os 2 3 os.rmdir("hello")
Application 1: Make a backup of the file (that is, copy the file), which is not suitable for the backup of large files.
Steps:
1. Get the file name to be copied (xxx.txt) Enter through input()
2. Open the file ("r" )
3. Create a file xxx[copy].txt
4. Read data from the original file
5. Write the read file data to a new file
6. Close both files
1 f_old_name = input( " Please enter the name of the file to be backed up (add a suffix): " ) 2 3 f_read = open(f_old_name, " r " ) 4 5 position = f_old_name.rfind( " . " ) 6 7 f_new_name = f_old_name[0:position] + " [copy] " + f_old_name[position:] 8 9 f_write = open(f_new_name, " w " ) 10 11 content = f_read.read() 12 13 f_write.write(content) 14 15 f_read.close() 16 f_write.close()
Upgraded version, suitable for backup and reading of large files
1 f_old_name = input( " Please enter the name of the file to be backed up (add a suffix): " ) 2 3 f_read = open(f_old_name, " r " , encoding= ' UTF-8 ' ) #Add when the file contains Chinese encoding='UTF-8' 4 5 position = f_old_name.rfind( " . " ) 6 7 f_new_name = f_old_name[0:position] + " [copy] " + f_old_name[position:] 8 9 f_write = open(f_new_name, " w ") 10 11 # Improve 12 with loop while True: 13 content = f_read.read(1024) #Read 1024 bytes at a time 14 15 if len(content) == 0: #Exit the loop when the read content is empty 16 break 17 18 f_write .write(content)
19 f_read.close() 20 f_write.close()
Application 2: Batch Modify File Names
1 #Get a file name to be renamed 2 folder_name = input( " Please enter the name of the folder: " ) 3 4 #Get all the file names in that folder 5 file_names = os.listdir(folder_name) 6 7 #The first One method 8 os.chdir(folder_name) 9 10 for file_name in file_names: 11 print (file_name) 12 os.rename(file_name, " produced by Jingdong- " + file_name) 13 14 15 #The second method 16 for file_name in file_names: 17 #print(file_name) 18 old_file_name = "./"+ folder_name + "/" +file_name 19 new_file_name = "./"+folder_name+"/"+"[京东出品]-"+file_name 20 os.rename(old_file_name, new_file_name)