第三章

    xiaoxiao2021-03-25  128

    第10题 #!/usr/bin/env pythonimport oslineEnd = os.linesepprint"Please Input Any Name Of File:",fileName = raw_input()try: #chech file exists or not newFile = open(fileName,'w')except: print "File %s Has Existed Or Open Fail!" % fileName exit()else: print "Please Input Any Text , '.' To Quit:",textForFile = [] #get string of filewhile True: chs = raw_input() if chs == '.': break else: textForFile.append(chs)newFile.writelines(['%s%s'%(lines,lineEnd) for lines in textForFile])newFile.close()print "Done!"

    #!/usr/bin/env pythonimport oswhile True: print "Please Input File's Name:", fileName = raw_input() if os.path.exists(fileName): break else: print "File %s Don't Exist.Please Input Thr Truth Name."newFile = open(fileName,'r')for lines in newFile: print lines,newFile.close()

    第11题 #!/usr/bin/env pythonimport oswhile True: print "Please Input File's Name:", fileName = raw_input() if os.path.exists(fileName): break else: print "File %s Don't Exist.Please Input Thr Truth Name."newFile = open(fileName,'r')for lines in newFile: lines.rstrip() print linesnewFile.close()

    第12题

    #!/sur/bin/env pythonimport oslineEnd = os.linesepprint "A:Creat A New File"print "B:Read A File That's Had Existed."chooseLetter = raw_input(">")if chooseLetter == 'A': while True: print"Please Input The File's Name:", fileName = raw_input() if os.path.exists(fileName): print "File %s Has Existed.Please Input Truth Name." else: break textForFile = [] print "Please Input Any Text,'.' To Quit." while True: chs = raw_input() if chs == '.': break else: textForFile.append(chs) newFile = open(fileName,'w') newFile.writelines(['%s%s' % (X,lineEnd) for X in textForFile]) newFile.close() print "Done." exit()elif chooseLetter == 'B': while True: print "Please Input The Name Of File:", existFile = raw_input() if os.path.exists(existFile): break else: print "Can't Find File %s,Please Input Truth Name."%existFile fileForRead = open(existFile,'r') for lines in fileForRead: print lines, fileForRead.close() print"Done." exit()else: print"Done." exit()

    转载请注明原文地址: https://ju.6miu.com/read-15201.html

    最新回复(0)