from sys import argv sys模块导入argv
script,filename = argv 解包unpack
print(txt.read()) 读取文本文件
target = open(filename,’r’) 打开文件‘’r‘ 表示real还有‘‘’w’write 等具体见pydoc
target.write(“%s\n%s\n%s\n” %( 转义序列和格式化字符串
target.close() 脚本末尾要加 .close
from os.path import exists exists看是否存在 true false
open(to_file, ‘w’).write(open(from_file).read())
def rewind(f):
f.seek(0) seek函数是文件处理里面的 具体pydoc
assert 断言 assert语句是一种插入调试断点到程序的一种便捷的方式。 格式assert expression
pass pass语句什么也不做,一般作为占位符或者创建占位程序,pass语句不会执行任何操作,比如: while False: pass
yield generator(生成器)
lambda 匿名函数 lambda x: print x

本文介绍了使用Python进行文件操作的基本方法,包括文件的打开、读取、写入及关闭等,并探讨了Python中的一些高级特性如assert断言、pass语句、yield生成器和lambda匿名函数等的应用。
1438

被折叠的 条评论
为什么被折叠?



