易截截图软件、单文件、免安装、纯绿色、仅160KB

Python入门的36个例子 之 27

源代码下载:下载地址在这里
e.g.1
# 030
aFile = file(r'C:\temp.txt', 'a')
aFile.write('又添加了一行。')
aFile.close()

output:
e.g.2
# 030
aFile = file(r'C:\temp.txt', 'a')
aFile.write('又添加了一行。')
aFile.close()

output:
e.g.3
实现根据原始文件有没有最后一行空行的情况来进行“完美添加”。
# 031
aFile = file(r'C:\temp.txt', 'r')
lastLine = ''
while True:
line = aFile.readline()
if len(line) == 0:
break
# end of if
lastLine = line
# end of while
aFile.close()
aFile = file(r'C:\temp.txt', 'a')
if not lastLine.endswith('\n'): # 说明源文件没有一个空行,需要重新另起一行
aFile.write('\n')
# end of if
aFile.write('这是新添加的一行!')
aFile.close()

此时无论原始文件是e.g.1的样子还是e.g.2的样子,结果都是:


相关文档:

easy_install让python的包管理变得easy

Python中的easy_install工具很好用,它的作用类似于Php中的pear,或者Ruby中的gem,或者Perl中的cpan。
如果想使用easy_install工具,可以直接安装ez_setup.py
脚本,再python ez_setup.py(之前先要安装python):
安装完后,最好确保easy_install所在目录已经被加到PATH环境变量里:
Windows: C:\Python25\Scripts
Li ......

Python代码优化

Python代码优化--少打字小技巧
说明:增加代码的描述力,可以成倍减少你的LOC,做到简单,并且真切有力
观点:少打字=多思考+少出错,10代码行比50行更能让人明白,以下技巧有助于提高5倍工作效率
1. 交换变量值时避免使用临时变量:(cookbook1.1)
老代码:我们经常很熟练于下面的代码
temp = x
x = y
y = ......

Python模块学习

  StringIO的行为与file对象非常像,但它不是磁盘上文件,而是一个内存里的“文件”,我们可以将操作磁盘文件那样来操作StringIO。一个简单的例子,让你对StringIO有一个感性的认识: 1  #coding=gbk 2    3  import StringIO, cStringIO, sys 4    5  s ......

Python入门的36个例子 之 18

例1:
# _018
# This is a module (if write Chinese in a module, there will be a error)
def func1():
print 'This is function 1.'
def func2():
print 'This is function 2.'
def func3():
print 'This is function 3.'
# 019
# 使用“import”语句调用模块:
import _018_Module
_ ......

Python入门的36个例子 之 22

源代码下载:下载地址在这里
# 025
# 序列的神奇之处在于你可以使用相同的方式tuple、list和string
se = ['a', 'b', 'c', 'd']
li = ['a', 'b', 'c', 'd']
tu = ('a', 'b', 'c', 'd')
string = 'abcd'
print se[1]
print li[1]
print tu[1]
print string[1]
# 序列的另外一个神奇之处在于,你可以使用负数进行索 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号