Python 练习实例97

Python 100例 Python 100例

题目:从键盘输入一些字符,逐个把它们写到磁盘文件上,直到输入一个 # 为止。

程序分析:无。

程序源代码:

实例(Python 2.0+)

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
if __name__ == '__main__':
 from sys import stdout
 filename = raw_input('输入文件名:\n')
 fp = open(filename,"w")
 ch = raw_input('输入字符串:\n')
 while ch != '#':
 fp.write(ch)
 stdout.write(ch)
 ch = raw_input('')
 fp.close()

以上实例输出结果为:

输入文件名:
icodebangfile.txt
输入字符串:
icodebang 
icodebang
google
google#

实例中创建了 icodebangfile.txt 文件并向其写入 icodebang 和 google 两个字符串,查看 icodebangfile.txt 文件内容:

$ cat icodebangfile.txt 
icodebanggoogle

Python 100例 Python 100例

0 个评论

要回复文章请先登录注册