1.利用python读取文件
Python引入了with
语句来自动帮我们调用close()
方法
(1)读取指定路径下的文件
with open(‘/path/to/file‘, ‘r‘) as f: print(f.read())
(2)写文件
with open(‘/Users/michael/test.txt‘, ‘w‘) as f: f.write(‘Hello, world!‘)
2.列表生成式
li = [i ** 2 for i in range(1,11)] print(li)
输出:
原文:https://www.cnblogs.com/luckyplj/p/13132525.html