Obtaining more process information with the read_proc_files() function

The read_proc_files() method, defined on line 81, follows a similar pattern to what was discussed in the preceding section. Essentially, on line 88, we iterate through all of the open files associated with the process and, using the getattr() method, attempt to extract information about each open file, such as its path and mode.

We return the file_details list after extracting all values for each open file and inserting the data into the file_details list:

081 def read_proc_files(proc):
082 """
083 Read file properties from a process.
084 :param proc: An object representing a running process.
085 :return file_details: a list containing process details.
086 """
087 file_details = []
088 for handle in proc.open_files():
089 handle_items = {}
090 handle_items['fd'] = getattr(handle, 'fd', None)
091 handle_items['path'] = getattr(handle, 'path', None)
092 handle_items['position'] = getattr(
093 handle, 'position', None)
094 handle_items['mode'] = getattr(handle, 'mode', None)
095
096 file_details.append(handle_items)
097
098 return file_details