0

For example, my folder structure is:

  • original/dev
  • original/prd

(note that dev and prd are folders and not files)

I would like to check if within the original folder is there anything else besides my desired dev and prd folders. My problem is that this undesired folder could have any name.

What I had in mind for the first part:

assert os.path.exists(os.path.join('tests','testvars','dev')) # it works
assert os.path.exists(os.path.join('tests','testvars','prod')) # it works
# It does not work. Just an idea:
assert os.path.exists(os.path.join('tests','testvars',"anything besides prd && dev"))

As you can notice, this last part would throw an assert error other folders (besides prd and dev) exist.

Acorn
  • 24,970
  • 5
  • 40
  • 69
FFLS
  • 565
  • 1
  • 4
  • 19
  • 1
    Does this answer your question? [Getting a list of all subdirectories in the current directory](https://stackoverflow.com/questions/973473/getting-a-list-of-all-subdirectories-in-the-current-directory) – Random Davis Sep 21 '20 at 21:36
  • Why does `assert os.path.exists(os.path.join('tests','testvars','dev'))` work if it does not include a `original` component? - and my suggestion is the same as @RandomDavis...you should get a list of everything in the `original` directory, iterate over that list, and do whatever is appropriate if you get anything but `dev` and `prd`. – CryptoFool Sep 21 '20 at 21:48

1 Answers1

2

You should just do this:

import os
other_dir = [x for x in os.listdir(path) if x != 'dev' and x!='prd']

with this you'll end up with a list of all other directory content except dev and prod

Seyi Daniel
  • 2,259
  • 2
  • 8
  • 18
  • Building up from this answer and my need to use assert, I ended up using '''assert len([x for x in os.listdir(path) if x != 'dev' and x!='prd']) == 0'''. This will will throw an assert error if there is another folder besides "dev" and "prd". – FFLS Sep 22 '20 at 19:54