Why does the read () function of python file operation only take effect once?

f = open(self.path, "r+")                                                 
re_moro = re.compile(r"MORO X=(.+) Y=(.+) Z=(.+) RX=(.+) RY=(.+) RZ=(.+)")
re_head = re.compile(r"Head X=(.+) Y=(.+) Z=(.+) RX=(.+) RY=(.+) RZ=(.+)")
re_eye = re.compile(r"Eye X=(.+) Y=(.+) Z=(.+) RX=(.+) RY=(.+) RZ=(.+)")  
re_arm = re.compile(r"Arm ID=(.+) Angle=(.+)")                            
read_pos_arm = re_arm.findall(f.read())                                   
read_pos_wheel = re_moro.findall(f.read())                                
read_pos_head = re_head.findall(f.read())                                 
read_pos_eye = re_eye.findall(f.read())                                   
self.write_data(read_pos_wheel, self.wheel_data)                          
self.write_data(read_pos_head, self.head_data)                            
self.write_data(read_pos_eye, self.eye_data)                              
self.write_data(read_pos_arm, self.arm_data)                              

I f got the file permissions, and my re is all right.
Why only read_pos_arm can get the data, the rest will be empty. Put read_pos_wheel in front, wheel can again, and the others are empty. What happened in it?

Nov.17,2021

call read () to read the entire file and leave the read cursor at the end of the file (there is nothing more to read). If you want to read a certain number of rows readline (), readlines () at a time or use row iteration for line in handle: .

you can use seek (0) to return the read cursor to the beginning of the file (python.org/2.4/lib/bltin-file-objects.html" rel=" nofollow noreferrer "> document is here ). If you know that the file will not be too large, you can also save the read () output to a variable and use it in the findall expression.

>>> a = open('file.txt')
>>> a.read()
-sharpoutput
>>> a.seek(0)
>>> a.read()
-sharpsame output

Don't forget to close the file after PS: is complete;)


finished reading. Zsbd


listen to what is said in the upper two layers clearly. It is recommended to use with :

.
with open(self.path, 'r+') as f: 
    content = f.read()
    re_moro = re.compile(r'MORO X=(.+) Y=(.+) Z=(.+) RX=(.+) RY=(.+) RZ=(.+)')
    re_head = re.compile(r'Head X=(.+) Y=(.+) Z=(.+) RX=(.+) RY=(.+) RZ=(.+)')
    re_eye = re.compile(r'Eye X=(.+) Y=(.+) Z=(.+) RX=(.+) RY=(.+) RZ=(.+)')  
    re_arm = re.compile(r'Arm ID=(.+) Angle=(.+)')                            
    read_pos_arm = re_arm.findall(content)                                   
    read_pos_wheel = re_moro.findall(content)                                
    read_pos_head = re_head.findall(content)                                 
    read_pos_eye = re_eye.findall(content)                                   
    self.write_data(read_pos_wheel, self.wheel_data)                          
    self.write_data(read_pos_head, self.head_data)                            
    self.write_data(read_pos_eye, self.eye_data)                              
    self.write_data(read_pos_arm, self.arm_data)
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e45dbd-43ec2.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e45dbd-43ec2.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?