Overview:
This procedure is mainly to imitate "python core programming" in 3.6--the first Python program, and on the basis of some minor improvements, while the improvement requirements are derived from the third chapter of the after-school exercises.
One of the core issues of this blog is how we avoid duplicating the wheels.
Textbook source program:
There is not much explanation for the source program in the textbook, because the textbook itself is very clear and easy to understand.
Improve your needs:
1. You can create a new file;
2. Can read the new file;
3. Allows users to choose to create or read files.
See above the demand, can your first feeling is, oh, this is too simple. is not to put the code previously written here to copy and paste it, ah, the most is more than one if the judge, what is difficult?
Of course I do not deny that this is indeed a simple procedure. But from the good code quality, can not be simple copy and paste so simple oh. Because copying the code into your own program, which can lead to a significant reduction in the reusability of the code, may not have any effect on the applet, but for some large systems it can be a fatal injury, because it can be extremely difficult to maintain. Well, say so much, then come and see how I solve this demand.
Ideas:
I keep the files that were created and read the files before, and then use the code in the third file makenreadtextfile.py to invoke the methods in the first two files.
Code Analysis:
makenreadtextfile.py
#!/usr/bin/env python ' makenreadtextfile.py--Create or read a text file ' import subprocess# call Maketextfile.pydef call_m Ake (): cmd = ["Python", "maketextfile.py"] result = Subprocess.call (cmd, 0, none, none, none, none) return re sult# call Readtextfile.pydef call_read (): cmd = ["Python", "readtextfile.py"] result = Subprocess.call (cmd, 0, None, none, none, none) return result# get the Choose to read or make a text Filechoose = Raw_input (' Enter The Choose ( r/m) ' If choose = = ' m ': call_make () elif Choose = = ' m ': call_make () elif Choose = = ' R ': Call_read () Elif Choose = = ' R ': call_read () Else: print ' Your choose is error! '
As you can see, there are several places that are more special:
1.import subprocess
After you Baidu will know subprocess is to open a new thread and communicate with it.
2.result = Subprocess.call (cmd, 0, none, none, none, none)
you should know by 1. , this is what is being communicated.
Of course, if we just make a complete reservation on the code in the book, we may not be able to achieve the desired effect. So, some minor changes need to be made.
maketextfile.py
#!/usr/bin/env python ' maketextfile.py--Create Text file ' Import Osls = Os.linesepdef make_text_file (): # Get Filenam E while True: fname = raw_input (' Enter file name: ') if Os.path.exists (fname): print "ERROR: '%s ' already exists "% fname else: break # Get file content (text) lines all = [] print" \nenter lines ('. ') by ITESLF to quit). \ n " #loop until user terminates input while True: entry = raw_input (' > ') if ENT ry = = '. ': Break Else: all.append (entry) # write lines to file with proper line-ending fobj = op En (fname, ' W ') fobj.writelines (['%s%s '% (x, LS) for X "all]) fobj.close () print ' done! ' if __name__ = = "__main__": make_text_file ()
readtextfile.py
#!/usr/bin/env Python ' readtextfile.py--read and display text file ' Def read_text_file (): # get filename fname = r Aw_input (' Enter filename: ') # attemp to open file for reading try: fobj = open (fname, ' R ') except IOError , E: print "* * * File Open error:", e Else: # Display contents to the screens for eachline in fobj: p Rint Eachline, fobj.close () if __name__ = = "__main__": read_text_file ()
Operating Effect:
Improvements to "first Python program"