Python Practice Example 97

Python 100例Python 100 Examples

Title:Input some characters from the keyboard, write them one by one to a disk file, until a#is entered.

Program analysis:None.

Program source code:

Example (Python 2.0+)

#!/usr/bin/python # -*- coding: UTF-8 -*- if __name__ == '__main__': from sys import stdout filename = raw_input('Enter file name:\n') fp = open(filename,"w") ch = raw_input('Enter string:\n') while ch != '#': fp.write(ch) stdout.write(ch) ch = raw_input('') fp.close()

The output result of the above example is:

输入文件名:
examplefile.txt
输入字符串:
example   
example
google
google#

In the example, the file examplefile.txt was created and two strings, example and google, were written to it. View the content of the examplefile.txt file:

$ cat examplefile.txt 
examplegoogle

Python 100例Python 100 Examples

Other Extensions