Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9259955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:46:42+00:00 2026-06-18T12:46:42+00:00

I have written a small program that will print all the files and directories

  • 0

I have written a small program that will print all the files and directories inside the path specified by me. The source code is:

import os
import glob

class FolderStats:
    targetdir = ""

    def __init__(self, dirpath = None):
        targetdir = dirpath
        totalfiles = 0
        totalsubfolders = 0

    def FolderIterator(self):
        print self.targetdir
        listing = os.listdir(self.targetdir)
        for infile in listing:
            print "current file is: %s" % (infile)

if __name__ == '__main__':
    Obj = FolderStats(raw_input('Enter your path: '))
    Obj.FolderIterator()

The above code is not executing. I am getting an error in the method FolderIterator: when the print command is executed, it prints nothing. <targetdir> no more contains the path supplied by me. Why is it so?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:46:43+00:00Added an answer on June 18, 2026 at 12:46 pm

    In your __init__ you need to use self.targetdir instead of targetdir

    >>> class Test:
        var = 1
        def __init__(self):
            var = 2         
            print self.var  # Object variable
            print var       # Local variable
        def func(self):
            print self.var
            print var       # this will fail, because there's no local var in this scope
    
    
    >>> a = Test()
    1
    2
    >>> a.func()
    1
    
    Traceback (most recent call last):
      File "<pyshell#12>", line 1, in <module>
        a.func()
      File "<pyshell#10>", line 9, in func
        print var
    NameError: global name 'var' is not defined
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Below is a small program that I have written to count the number of
Let say I have written a small program that reads file_A and file_B as
I have written a small program to validate 3 data files and convert them
I have written a small GUI based vb.net program that speaks to embedded devices
I have written a small 16-bit assembly program that writes some values in some
I have written a small program and server to upload large files over HTTP
I have written a small program, while reading a book about swing, that creates
I have written a very small C# program, that uses a very small SQL
I have written a small makefile for a few simple C programs that compiles
I have written a small program just like hello the world , everything was

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.