I am using os.path to list a directory contents. In my code I want to do this whether the path passed as a string has a filename or if it is just a path. If it has a filename attached then lisdir works if not then it doesn't work.
For example if it is /home/me/Documents/file.jpg then it works, if it is just /home/me/Documents, it does nothing.
I have tried wrapping the path string in different methods such as join(path,""), dirname(path) and path(path) to convert it to a path object as maybe it won't work because it is a string.
My code looks like:
if isfile(path):
contents = listdir(dirname(path))
else:
contents = listdir(path)
print(contents)
I expected it to list the files in both cases, but it doesn't when it is a path.
Testing on another directory:
chris@DJANGO-DEV-1:~/Documents/mydir$ ls
file1.txt file2.txt file3.txt file4.txt file5.txt
In [2]: filecleanup("/home/chris/Documents/mydir/file1.txt")
/home/chris/Documents/mydir/file1.txt
/home/chris/Documents/mydir
['file4.txt', 'file1.txt', 'file3.txt', 'file5.txt', 'file2.txt']
In [3]: filecleanup("/home/chris/Documents/mydir")
[LISTS DIRECTORY BELOW]
In [4]: filecleanup("/home/chris/Documents/mydir/")
['file4.txt', 'file1.txt', 'file3.txt', 'file5.txt', 'file2.txt']