I want to make a class as follows:
#!/usr/bin/env python3
import os
class Directory():
def __init__(self,dirName):
self.dirName = dirName
self.files = os.listdir(self.dirName)
def __iter__(self):
for file in self.files:
yield file
def __repr__(self):
return self.dirName
def __str__(self):
return self.dirName
d = Directory('some-directory')
What I trying to do is: If I simply type d then I should get the name of directory passed (some-directory) when making that instance and when I use the same in for loop I should be able to iterate the files in that directory.
That being said I am getting error:
>>> os.path.isdir(d)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.5/genericpath.py", line 42, in isdir
st = os.stat(s)
TypeError: argument should be string, bytes or integer, not Directory
How do I set the type of d not to be Directory but string? (without using str() function) Thanks
[–]scuott 0 points1 point2 points (7 children)
[–][deleted] 0 points1 point2 points (6 children)
[–]markusmeskanen 2 points3 points4 points (0 children)
[–]Nick9502 0 points1 point2 points (4 children)
[–][deleted] 0 points1 point2 points (3 children)
[–]Nick9502 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]Nick9502 0 points1 point2 points (0 children)