1

I'm in one location i.e. 'c:/program files/java' and I want to jump two levels down without having to specify the subfolders i.e. I want to move to 'c:/program files/java/7.0/jre/bin' without specifying '/7.0/'.

A snippet I'm using is:

import os
os.chdir('c://program files//java')

os.getcwd()

'c:/program files/java'

Now I want to use os.chdir() to move to '/7.0/jre' so os.getcwd() is 'c://program files//java/7.0/jre'

without having to specify '7.0' i.e. os.chdir('.\**7.0**\jre')

Does anyone have any suggestions?

0

1 Answer 1

3

You can use glob.glob:

import glob
import os

os.chdir('c:/program files/java')
os.chdir(glob.glob('*/jre')[0])

Above code will change working directory to c:/program files/java/*/jre. In case there are multiple java directory, and you want to go to specific directory (for example, to the newest version directory), you should manipulate the return value of glob.glob().

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.