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.

2
  • 1
    Does this answer your question? Getting a list of all subdirectories in the current directory Commented Sep 21, 2020 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. Commented Sep 21, 2020 at 21:48

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

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".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.