Return the next line of text including a newline

Info icon This preview shows pages 9–16. Sign up to view the full content.

return the next line of text, including a newline (return) character at the end Returns empty string when file empty Using for statement can also have the same result >>> f = open(‘message.txt‘) >>> for line in f: ... print (line) CSCI2040 INTRODUCTION TO PYTHON 11
Image of page 9

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Recovering from Exceptions File I/O operations can generate exceptions, an IOError Exception handling can prevent these errors try: f = open('input.txt') except IOError as e: print ('unable to open the file with error: "{}"'.format(e.args[-1])) else: print ('continue with processing') f.close() print ('continue') >>> unable to open the file with error: "No such file or directory" continue CSCI2040 INTRODUCTION TO PYTHON 12
Image of page 10
Handling files A better way is using with Ensure file is closed when the block inside with is exited with open('hello.txt', 'w', encoding='utf-8') as outf: # perform file operation outf.write("Hello world") print ('continue with processing‘) CSCI2040 INTRODUCTION TO PYTHON 13
Image of page 11

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Operating System Command Useful OS command can be executed from python by including os module >>> import os >>> os.remove("gone.txt") # removing file >>> os.curdir # get current directory '.‘ >>> os.rename('oldfile.ext', 'newfile.ext') >>> CSCI2040 INTRODUCTION TO PYTHON 14
Image of page 12
Standard I/O print writes characters to a file normally attached to display window Input functions read from a file attached to keyboard These files can be accessed through sys module Input file : sys.stdin , output file: sys.stdout , error messages: sys.stderr stderr normally goes also to stdout CSCI2040 INTRODUCTION TO PYTHON 15
Image of page 13

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

CSCI2040 INTRODUCTION TO PYTHON Standard I/O Can change these settings through sys import sys sys.stdout = open('output.txt', 'w') sys.stderr = open('error.txt', 'w') print ("see where this goes“) print (5/4) print (7.0/0) sys.stdout.close() sys.stderr.close() In output.txt see where this goes 1 In error.txt Traceback (most recent call last): File "try1.py", line 6, in <module> print (7.0/0) ZeroDivisionError: float division by zero 16
Image of page 14
CSCI2040 INTRODUCTION TO PYTHON OS functions exit terminate a running Python program – sys.exit(“message”) sys.argv
Image of page 15

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Image of page 16
This is the end of the preview. Sign up to access the rest of the document.
  • Fall '17
  • Ruby

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern