Some extensions are made according to the content of chapter 16.
For example, if a file exists, it is appended to the file and created if it does not exist.
It also draws on a method similar to the cat <<eof > test in the shell command, prompting the user to enter a trailing character.
Now there is a small pit, how to use Python to read a file of the number of rows, the original Os.system ("Wc-l filename") is OK, but how to operate under Windows? Fill it back.
#!/usr/bin/env python#-*-coding:utf-8-*-from sys import argvfrom OS import pathscript, filename = argvif path.exists (fi Lename): print "we ' re going to erase%r."% filename t_module = "A" Else: print "We'll create%r."% filename t_module = "W" end_eof = raw_input ("Please give me an EOF string") with open (Filename,t_module) as fo: Line_Count = 1 while True: line_content = raw_input ("Please input%d line string:\n"% line_count) if line_content = = End_e Of: break Else: fo.write (line_content) fo.write ("\ n")
Lpthw stupid way to learn Python Chapter 16