Grokbase Groups Python tutor May 2011
FAQ
Sorry I am completely new at python and don't understand why this function is returning an empty dictionary. I want it to take a list of files open them then return the number of characters as the value and the file name as the key.
def fileLengths(files): d = {} files = [] for file in files: reader = open(file) for line in reader: char = sum(len(line)) d[file] = char reader.close return d
Thank you sorry I don't understand what I am doing wrong.
-Clara
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110509/d9093ce1/attachment-0001.html>

Search Discussions

  • Noah Hall at May 10, 2011 at 6:38 am

    On Tue, May 10, 2011 at 5:27 AM, Clara Mintz wrote:
    Sorry I am completely new at python and don't understand why this function
    is returning an empty dictionary. I want it to take a list of files open
    them then return the number of characters as the value and the file name as
    the key.
    def fileLengths(files):
    ?? ?d = {}
    ?? ?files = []
    Here's why. You take the name "files" and assign it to an empty list,
    so you lose the "files" that you sent to the function.
    files = ["file_1.txt", "file_2.doc"]
    files
    ['file_1.txt', 'file_2.doc']
    files = []
    files
    []
    You don't need that line at all, so remove it, and see what happens. :)
    ?? ?for file in files:
    ?? ? ? ?reader = open(file)
    ?? ? ? ?for line in reader:
    ?? ? ? ? ? ?char = sum(len(line))
    As a side not, this doesn't need to be "sum" in this case - the len is
    simply len, throwing away what it previously was. In fact, this
    probably isn't what you want. At any one point, char is the length of
    a single line, not the sum of every length of every line in the file.
    What you want is something that takes the length of each line, and add
    it to the sum. A simple way would be to do
    sum(len(line) for line in file)
    ?? ? ? ?d[file] = char
    ?? ? ? ?reader.close
    ?? ?return d
    Thank you sorry I don't understand what I am doing wrong.
    -Clara
    _______________________________________________
    Tutor maillist ?- ?Tutor at python.org
    To unsubscribe or change subscription options:
    http://mail.python.org/mailman/listinfo/tutor
    Hope this helped.
  • Alan Gauld at May 10, 2011 at 8:15 am
    "Noah Hall" <enalicho at gmail.com> wrote
    What you want is something that takes the length of each line,
    and add it to the sum. A simple way would be to do
    sum(len(line) for line in file)
    And if you just want the total count for the file an even
    simpler way is to use file.read()

    count = len(file.read())


    One other thing to consider is whether you want to include
    line feed characters in the count...

    HTH,


    --
    Alan Gauld
    Author of the Learn to Program web site
    http://www.alan-g.me.uk/
  • Dave Angel at May 10, 2011 at 9:55 am

    On 01/-10/-28163 02:59 PM, Clara Mintz wrote:
    Sorry I am completely new at python and don't understand why this function is returning an empty dictionary. I want it to take a list of files open them then return the number of characters as the value and the file name as the key.
    def fileLengths(files): d = {} files = [] for file in files: reader = open(file) for line in reader: char = sum(len(line)) d[file] = char reader.close return d
    Thank you sorry I don't understand what I am doing wrong.
    -Clara
    The first thing you're doing is wordwrapping your program fragment. It
    makes the code hard to read, and some aspects impossible, as we can't
    tell what parts were indented by how much.

    The second thing is clobbering your input parameter. files=[] will
    obliterate whatever argument was passed to that function.

    You will have more problems after that:
    sum() won't work on an integer, so it's not clear why you're calling
    len() on the line.
    You're returning d from the function, but nothing in the function
    ever inserts anything into it. So it will clearly be empty.

    print is your friend. A couple of carefully placed print statements
    would reveal these problems.

    DaveA

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouptutor @
categoriespython
postedMay 10, '11 at 4:27a
activeMay 10, '11 at 9:55a
posts4
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase