day_04 遍历整个列表 我们创建列表时,需要输出整个列表,但是通常列表会很长,包含很多元素,当列表长度发生变化是,都必须修改代码.通过for循环,我们可以很轻易地输出整个列表. #遍历整个列表 创建一个水果列表 fruits = ['apple','orange','banana','cherry'] for i in fruits: print(i) apple orange banana cherry 在for循环中执行更多的操作 对每个水果都打印一份信息,表示我太喜欢吃这个水果了 fr…
先说一下IO发生时涉及的对象和步骤.对于一个network IO (这里我们以read举例),它会涉及到两个系统对象,一个是调用这个IO的process (or thread),另一个就是系统内核(kernel).当一个read操作发生时,该操作会经历两个阶段: 1)等待数据准备 (Waiting for the data to be ready) 2)将数据从内核拷贝到进程中(Copying the data from the kernel to the process) 阻塞IO(block…
读文件 with open('/path/to/file', 'r') as f: print(f.read()) 调用read()会一次性读取文件的全部内容,read()函数里面可以传入每次最多读取的字节大小,另外调用readline()函数可以每次读取一行内容,调用readlines()一次读取所有内容并按行返回list f = open(''/path/to/file', 'r') for line in f.readlines(): print(line.strip()) # 把末尾的'…