Search code examples
pythonpython-os

How to find file location using the regular expression ("*" in the path)?


Following cp linux command is working fine to find a file "/home/temp/test-1.34.56/sample" to current location

Shell command: Working fine

cp "/home/temp/test-*/sample" "./"

Python code: It not working using os.rename

os.rename("/home/temp/test-*/sample", "./")

any other options ?


Solution

  • Tried glob module from above @Wjandrea, @Tom, @Treuss comments. it worked

    from glob import glob
    import shutil
    
    
    actual_path = glob("/home/temp/test-*/sample")
    if actual_path:
         shutil.move(actual_path[0],"./")