Fer said:
Chapter 7 duties
This course will become read-only in the near future. Tell us at community.p2pu.org if that is a problem.
An important part of programming is knowing how to work directly with files. Lets review the following Pythonic tools for working with files:
The open() function takes a filename and path as input and returns a file object.
f = open('/path/to/file', 'r')
The second parameter for the open() function is the file permission mode and is entered in the form of read (r), write (w), or append (a). If no mode parameter is passed, the mode defaults to read. To open binary files, append 'b' to the mode parameter, e.g. read binary ('rb').
The read() method will return the contents of the file as a string. Be careful that your computer has enough memory to read the entire file at once. read() accepts an optional parameter called 'size' that restricts Python to reading a specific portion of the file.
f.read()
The readline() and readlines() methods read one and multiple lines of a file respectively.
readline() returns one line of the file as a string with a new line character ( '\n' ) at the end. If readline() returns an empty string, the end of the file has been reached.
>>> f.readline() 'This is the first line.\n' >>> f.readline() 'This is the second line.\n' >>> f.readline() ' '
readlines() returns all of the lines in a file as a list. Each line in the list will have a newline character ( '\n' ) at the end.
>>> f.readlines() ['This is the first line\n', 'This is the second line\n']
The write() method takes a string as input and writes it to a file. Other data types must be converted to strings before they can be written to a file.
>>> data = 'This string will now be written to a file.' >>> f.write(data) 42
When you are done reading from or writing to a file you can close() the file and free memory.
f.close()
After closing a file, attempts to access the file will fail.
f.readlines() Traceback (most recent call last): File "<pyshell#14>", line 1, in <module> f.readlines() ValueError: I/O operation on closed file.
The Pickle module enables developers to create persistent data objects. Any object or data in Python, even sometimes Python code, can be written to files using Pickle.
Pickled objects are converted to string representations and then unpickled later by converting them to their original types.
>>> import pickle >>> pickle_object = ['Some data to be pickled', 12345, ('apples', 'butter')] >>> pickle_file = open('pickle_file', 'wb') >>> pickle.dump(pickle_object, pickle_file) To retrieve pickled data, use pickle.load(). >>> pickled_data = open('pickled_file', 'rb') >>> de_pickled = pickle.load(pickled_data) >>> print(de_pickled) ['Some data to be pickled', 12345, ('apples', 'butter')]
Python Docs
Please complete the learning excercises in the Python for Informatics textbook. Post your code work to the web and a link to your files in this discussion.
Here are the three exercises from chapter 7: http://pastebin.com/5AXZT7TN
'''
Exercise 7.1 Write a program to read through a file and print the contents of the file (line by
line) all in upper case. Executing the program will look as follows:
'''
fhand=open('mail.txt')
for line in fhand:
print str.upper(line)
'''
Exercise 7.2 Write a program to prompt for a file name, and then read through the file and look
for lines of the form:
X-DSPAM-Confidence:
0.8475
When you encounter a line that starts with “X-DSPAM-Confidence:” pull apart the line to
extract the floating point number on the line. Count these lines and the compute the total of
the spam confidence values from these lines. When you reach the end of the file, print out the
average spam confidence.
'''
fhand=open('fis.txt')
count=0
sp=0
for line in fhand:
words=line.split()
if words[0]=='X-DSPAM-Confidence:':
count=count+1
sp=sp+float(words[1])
elif words[0]!='X-DSPAM-Confidence:':
continue
sp=sp/count
print "Count=",count,"Avg spam confidence=",sp
'''
Sometimes when programmers get bored or want to have a bit of fun, they add
a harmless Easter Egg to their program (en.wikipedia.org/wiki/Easter_egg_(media)).
Modify the program that prompts the user for the file name so that it prints a funny message
when the user types in the exact file name ’na na boo boo’. The program should behave normally
for all other files which exist and don’t exist.
'''
a= raw_input('Enter a file name: ')
if a == 'na na boo boo':
print " NA NA BOO BOO TO YOU - You have been punk'd "
fhand = open(str(a))
count = 0
for line in fhand:
if line.find('Subject:') != -1:
count += 1
print 'There were %d subject lines in %s' % (count, a)
Here are mine. I like to use the 'with' statement for opening files, as it makes the try...except blocks a little more tidy. It also prints out the specific IOError exception, if there is one, thus providing a little more feedback to the user about what might have gone wrong.
Here's mine:
http://pastebin.com/AExXE8Nz
It's for Python 3, and instead of using local files, it prompts for the url of a file and fetches it from the web.
https://github.com/DamionKing/P2PU/tree/master/Python-Programming-101\
Any ideas as to how I can improve my code?
My py4int exercise 7 code
Exercise 7.1
fname = raw_input('Enter the file name: ') try: fh = open(fname) except: print 'File cannot be opened:', fname exit() for line in fh : line = line.rstrip() print line.upper()
Exercise 7.2
fname = raw_input('Enter the file name: ') try: fh = open(fname) except: print 'File cannot be opened:', fname exit() spam_lines = 0 spam_total = 0.0 for line in fh : if not line.startswith('X-DSPAM-Confidence:') : continue colon_pos = line.find(':') spam_total = spam_total + float(line[colon_pos+1:]) spam_lines = spam_lines + 1 print 'Average spam confidence:', spam_total/spam_lines
Exercise 7.3
fname = raw_input('Enter the file name: ') if 'na na boo boo' == fname : print "NA NA BOO BOO TO YOU - You have been punk'd!" exit() try: fh = open(fname) except: print 'File cannot be opened:', fname exit() count = 0 for line in fh : if line.startswith('Subject:') : count = count +1 print 'There were', count, 'subject lines in', fname
7.1
fname = raw_input('Enter a file name: ') try: fhand = open(fname) except: print 'File cannot be opened:', fname exit() for line in fhand: print line.rstrip().upper() fhand.close()
7.2
fname = raw_input('Enter the file name: ') try: fhand = open(fname) sum = 0.0 num = 0 nline = 0 for line in fhand: nline = nline + 1 if line.startswith('X-DSPAM-Confidence:') : a = line.split(':') try: a = line.split(':') try: sum = sum + float(a[1]) num = num + 1 except: print 'Missing floating point number in line', nline fhand.close() if num > 0: print 'Average', sum/num else: print 'Not lines with X-DSPAM_Confidence found in', fname except: print 'File cannot be opened:', fname
7.3
fname = raw_input('Enter the file name: ') if fname == 'na na boo boo': print "NA NA BOO BOO TO YOU - You have been punk'd!" else: try: fhand = open(fname) count = 0 for line in fhand: if line.startswith('Subject:') : count = count + 1 fhand.close() print "There were", count, "subject lines in", fname except: print 'File cannot be opened:', fname exit()