1

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']
OptimusPrime
  • 777
  • 16
  • 25
  • what does `dirname` return in your first case? exactly? – njzk2 Jul 06 '19 at 05:20
  • The path so in my example it would return "/home/me/Documents" – OptimusPrime Jul 06 '19 at 05:22
  • odd. I tested locally and I have no issue with using listdir on a string. Next step is to include a fully working sample, with imports and all, actual values that you get rather than "doesn't work", and the version of all the things you use (python, mainly, but if you have a special version of the os module, now is the time to mention it) – njzk2 Jul 06 '19 at 05:33
  • I think it is because the "/" was not appended onto the end of the path. I have done some testing on a test directory. In the context of where the code is running I no longer need to handle the condition where it is just a path anyway so I will move on. I still don't understand why it requires the "/" though because the dirname function converts it to a path without a slash for the first part of the condition anyway. If you could explain what is happening then it would be helpful. – OptimusPrime Jul 06 '19 at 06:04
  • what does "[LISTS DIRECTORY BELOW]" means? – njzk2 Jul 06 '19 at 23:21
  • It is listing the directory below the current one. – OptimusPrime Jul 07 '19 at 16:25
  • can you print the result of `isfile` for those inputs? it should return False for all directories, but if it returns True when the trailing / is missing, that would explain that. Also, is `mydir` a real folder, or a link? – njzk2 Jul 08 '19 at 01:32
  • If there is a trailing / then it resolves to false, if the / is omitted then it also resolves to false. "mydir" is a test directory that I created, it is a real folder. – OptimusPrime Jul 10 '19 at 06:10

1 Answers1

1

Try:

if isfile(path):
    contents = listdir(os.path.split(path)[0])
else:
    contents = listdir(path)
print(contents)
James Ashwood
  • 469
  • 6
  • 13
  • 1
    This was so long ago, I can't remember which project this was for. I think I might have found something in Djano which has signals for files being uploaded to the database and then I had some code to remove all the files first. Sorry, I can't say if this resolves the issue or not as I moved on from it a long time ago. – OptimusPrime Mar 16 '20 at 10:41
  • Okay, no worries, just happy to help! – James Ashwood Apr 28 '20 at 12:10