1

I'm trying to compare the sub-directories of two folders in Ubuntu by using Python 2.7.15's os.listdir() and os.path.isdir() in both directories, and I'm getting some odd results. Here is the directory structure:

super1
   sub1
   sub2
   sub3
super2
   sub1
   sub2
   sub4

Here is the code I have written (which is saved as test.py in the folder super1):

import os
import os.path
sourceFolders = [f for f in os.listdir('.') if os.path.isdir(f)]
targetFolders = [g for g in os.listdir('../super2') if os.path.isdir(g)]
for name in sourceFolders:
   print(name)
for name in targetFolders:
   print(name)

The output:

sub1
sub2
sub3
sub1
sub2

Issue is that for whatever reason, it's skipping sub4 from super2. Using os.path.isdir('../super2/sub4') returns true, and if I just assign targetFolders = os.listdir('../super2') sub4 is included in the list. But if I then run

for name2 in targetFolders:
   if os.path.isdir(name2):
      print(name2)

it skips sub4 again. I'm a bit stumped. Any ideas?

0 Answers0