for key, value in my_dict.items(): print(f"Key: {key}, Value: {value}")
异常exception
1 2 3 4 5 6 7 8
try: # 尝试执行的代码 result = 10 / 0 except Exception as e: # 处理所有非系统的异常 print(f"An error occurred: {e}") finally: print('始终处理')
文件
遍历目录walk
用于遍历目录树。这个函数可以生成文件夹中的文件名和子文件夹名
1 2 3 4 5
for current_path, dir_list, file_list in os.walk(path): for file_name in file_list: print('这是文件-%s' % os.path.join(current_path, file_name)) for dir_name in dir_list: print('这是目录-%s' % os.path.join(current_path, dir_name))
sd = root.findall('sd') for i in sd: print("find",i.attrib,i.find('ct').text) except Exception as e: print(e.args) finally: print("findtext 快速查找\nfindall 复杂查找")