Python Practice Example 99

Python 100例Python 100 Examples

Question:There are two disk files A and B, each containing a line of letters. It is required to merge the information in these two files (arranged in alphabetical order) and output it to a new file C.

Program analysis:None.

Program source code:

#!/usr/bin/python # -*- coding: UTF-8 -*- if __name__ == '__main__': import string fp = open('test1.txt') a = fp.read() fp.close() fp = open('test2.txt') b = fp.read() fp.close() fp = open('test3.txt','w') l = list(a + b) l.sort() s = '' s = s.join(l) fp.write(s) fp.close()

Before running the above program, you need to create test1.txt and test2.txt files in the directory where the script is executed.

After the above program is executed successfully, open the test3.txt file and you can see the content as shown below:

123456

Python 100例Python 100 Examples

Other Extensions