Define file open and read in a function,definefunction,import sysde
Define file open and read in a function,definefunction,import sysde
import sysdef open_file(file_name, mode): '''Open a file.''' try: the_file = open(file_name, mode) except(IOError), e: print 'Unable to open the file', file_name, 'Ending program.\n', e raw_input('\n\nPress the enter key to exit.') sys.exit() else: return the_filedef next_line(the_file): '''Return next line from the trivia file, formatted.''' line = the_file.readline() line = line.replace('/', '\n') return linetrivia_file = open_file('trivia.txt', 'r')title = next_line(trivia_file)print title
- Draw a line,drawline,from Tkinter
- Demonstrates creating new strings with a for loop,,mes
- Assignment to slices: Replace some items,assignmentsli
- Define and call a function inside another function,def
- Tuple indexing and slicing,tupleslicing,T = (1, 2, 3
- Filter demo: lambda function inside,filterlambda,foo =
热门文章:
相关内容
- Draw a line,drawline,from Tkinter
- Demonstrates creating new strings with a for loop,,message = ra
- Assignment to slices: Replace some items,assignmentslices,a = ['spa
- Define and call a function inside another function,defineinside,def
- Tuple indexing and slicing,tupleslicing,T = (1, 2, 3
- Filter demo: lambda function inside,filterlambda,foo = [2, 18
- Handle File reading Exception,handlereading,import systr
- Create a base class for exceptions defined by a module,exceptionsde
- Assign list to a list item,assignitem,L = ['Alread
- String literals span multiple lines in several ways.,literalsmultip
评论关闭