4

I am trying to teach myself how to make classes in Python. I am currently able to write a class that enables me to draw multiple shapes using the same class which is pretty basic.

I am having trouble with more advanced tasks using classes, for instance I would like to be able to make a class which includes functions such as how many files are in a particular directory.

I guess my question is How would I make a class that reads into a directory and tell me how many files are in the directory?

Thanks, sorry if it isnt clear

  • 1
    possible duplicate of [how to list all files of a directory in python](http://stackoverflow.com/questions/3207219/how-to-list-all-files-of-a-directory-in-python) – Nix Jan 23 '13 at 13:20
  • 1
    There is already a module that does it for you `os.` As well as a function `os.listdir.` A simple google search would do you well. – Nix Jan 23 '13 at 13:21
  • @Nix how is it a duplicate? and I know but say I wanted to do it in a class, would i just do the same thing? –  Jan 23 '13 at 13:22
  • 3
    Why would you need a class to list a directory? I understand that you are learning, but it's also pretty important to learn _when_ to write classes, not only _how_ to. – Lev Levitsky Jan 23 '13 at 13:26
  • Ignoring clarity issues, what have you tried? – martineau Jan 23 '13 at 13:34

3 Answers3

1

os.listdir() returns a list of contents of a directory. If I understood the question correctly.

So a class would hold a function wrapper for that function, if you would like to make a new class for it. Not sure what you mean with "reads into a directory"

class YourClass:

    def __init__(self):
         #initialize here

    def countFilesInDir(self,directory):
         return len(os.listdir(directory))

or something like that

Gjordis
  • 2,540
  • 1
  • 22
  • 32
  • i mean so that when the class is called it can look into the directory count the number of files and return the value. I know about the os module, if i used it in a class would it be used exactly the same way? –  Jan 23 '13 at 13:26
  • This isn't even proper python. – Nix Jan 23 '13 at 13:28
  • You don't call a class. You call methods of a class instance. (expert mode: There's `__call__`, but we don't want to talk about it now) – Matthias Jan 23 '13 at 13:28
  • @Gjordis: Please apply PEP-8 naming conventions. – Matthias Jan 23 '13 at 13:30
  • @Matthias Sure, I'll try to make cleaner code snippets in future. Just wrote a fast one to point in the right direction. – Gjordis Jan 23 '13 at 13:32
  • @Gjordis What would I put in the "init" function? –  Jan 23 '13 at 13:32
  • Initialization of variables for example, if you would like the class to remember the last counted directory or something. If the class has nothing, nothing needs to be initialized. – Gjordis Jan 23 '13 at 13:34
  • so if i was using the same directory I would write something like self.directory = "directory" then in the function put self.directory? –  Jan 23 '13 at 13:36
  • Basically yes. You could give the directory as parameter in the __init__ function instead of the counting function, and use the self.directory in the counting function. This would limit each instance of the class to one directory. Or does python have re-initialization? I don't know that. – Gjordis Jan 23 '13 at 13:38
  • Do I even need an init function if i wanted to use different directories? –  Jan 23 '13 at 13:48
  • I think it is part of the syntax, that it needs to be there. Like Nix said, i'm not very well accustomed to "the proper way of doing things". I just do them. __init__ is the constructor. In other languages it usually needs to be there in classes – Gjordis Jan 23 '13 at 13:49
1

The function you are looking for is os.listdir.

Given a directory /tmp/foo with files named bar and baz:

>>> import os
>>> os.listdir('/tmp/foo')
['baz', 'bar']

When you use os.listdir, you will also want to use os.path.isfile to test whether each entry you are dealing with is actually a file, since directory names are returned as well.

Mattie
  • 20,280
  • 7
  • 36
  • 54
1

It seems a bit overkill to use a class, but here's a starting point:

import os

class DirectoryLister(object):
    def __init__(self, *args):
        self.directory = os.listdir(*args)

    @property
    def count(self):
        return len(self.directory)

Used like:

dl = DirectoryLister('/home/jon')
print dl.directory
print dl.count

You may also want to offer other argument to filter out non-files, or potentially look at glob to do wild card matching, etc...

Jon Clements
  • 138,671
  • 33
  • 247
  • 280
  • I wouldnt just use a class for the one function, I already have some code that does this as well as other functions but was just interested how I would put it into a class –  Jan 23 '13 at 13:28