Posted on 2005-05-03
Last Modified: 2010-04-16
hi, i am trying to get the file size of files in the directories and sub directories ..... and i get this error
global name 'fs' is not defined ... and i dont need to use a global name .... any help will do thanks .....

def fsize():

def fileSize(a, dir, files):
   for fs in files:
      print dir, "Filesize = " + fs

def travFsize(fs):
   os.path.walk(fs, fileSize, None)
Question by:darrenteo82
    LVL 14

    Accepted Solution

    Here's a working example that prints the size of each file in your Windows directory:

    import os

    def fileSize(arg, dirname, names):
       for filename in names:
          filesize = os.path.getsize(os.path.join(dirname, filename))
          print dirname, filename, filesize

    def travFsize(fs):
       os.path.walk(fs, fileSize, None)


    Author Comment

    i am wondering if you might know for the Unix directory ?
    LVL 14

    Expert Comment

    It's exactly the same - just specify the directory in the last line:

    LVL 17

    Expert Comment

    darrenteo82 - does that do it for you?

    Bob Gailer
    Page Editor
    LVL 17

    Expert Comment

    As I read thru the list of abandoned questions I note the author of several is darrenteo82. I wonder what happened to him/her?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Live - One-on-One Python Help from Top Experts

    Solve your toughest problems, fast.
    Python experts are online now and ready to help you.

    This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
    Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
    Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
    Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now