#coding:utf-8 '''''cdays-4-exercise-6.py 文件基本操作 @note: 文件读取写入, 列表排序, 字符串操作 @see: 字符串各方法可参考hekp(str)或Python在线文档http://docs.python.org/lib/string-methods.html ''' f = open('cdays-4-test.txt', 'r') #以读方式打开文件 result = list() for line in f.readlines(): #依
参考网络上代码编辑而成,无技术含量,可自行定制: 目前亲测有效,若有待完善之处,还望指出! 强调:将此统计py脚本放置项目的根目录下执行即可. 1.遍历文件,递归遍历文件夹中的所有 def getFile(basedir): global filelists for parent,dirnames,filenames in os.walk(basedir): #for dirname in dirnames: # getFile(os.path.join(parent,dirname)) #递归
#用strip(),split()两个方法都可以判断空行 infile=open('/.../','r') outfile=open('/.../','w') for li in infile.readlines(): if li.split(): #if li.strip(): outfile.writelines(li) infile.close() outfile.close()
Python程序里的注释是很重要的.它们可以用自然语言告诉你某段代码的功能是什么.在你想要临时移除一段代码时,你还可以用注解的方式将这段代码临时禁用.接下来的练习将让你学会注释 : # A comment, this is so you can read your program later.# Anything after the # is ignored by python.print "I could have code like this." # and the comment
python 判断字符串中是否只有中文字符 学习了:https://segmentfault.com/q/1010000007898150 def is_all_zh(s): for c in s: if not ('\u4e00' <= c <= '\u9fa5'): return False return True