How to read star (*) as system command
Question: I have this code :

    
    
    > >>> import os
    > >>> os.chdir('/u01/APPLTOP/instance/domains/*.oracleoutsourcing.com/ICDomain/servers/IncentiveCompensationServer_1/logs')
    > Traceback (most recent call last):   File "<stdin>", line 1, in ?
    > OSError: [Errno 2] No such file or directory:
    > '/u01/APPLTOP/instance/domains/*.oracleoutsourcing.com/ICDomain/servers/IncentiveCompensationServer_1/logs'
    > >>>
    

Can you please let me know how can I read asterisk (*) as Linux system command
in python.


Answer: To simulate [shell path
expansion](http://bash.cyberciti.biz/guide/Path_name_expansion) of '*' (as
well as other glob special characters), you can use the
[glob](https://docs.python.org/2/library/glob.html) module:

    
    
    import glob
    glob_pattern = '/u01/APPLTOP/instance/domains/*.oracleoutsourcing.com/ICDomain/servers/IncentiveCompensationServer_1/logs'
    dir_paths = glob.glob(glob_pattern)
    

Now, assuming the above results with a single match (otherwise, it makes no
sense "chdir"ing ot it), you can do:

    
    
    dir_path, = dir_paths
    os.chdir(dir_path)
    

The assingment above fails if you get no matches, or multiple matches.

