Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to use bash functions inside my python script to allow me to locate a specific directory and then grep a given file inside the directory. The catch is that I only have part of the directory name, so I need to use the bash function find to get the rest of the directory name (names are unique and will only ever return one folder)

The code I have so far is as follows:

def get_tag(part_of_foldername):
    import subprocess
    import os
    p1 = subprocess.Popen(["find", "/path/to/directory", "-maxdepth", "1", "-name", "%s.*" % part_of_foldername, "-type", "d"], stdout=subprocess.PIPE)
    directory = p1.communicate()[0].strip('\n')
    os.chdir(directory)
    p2 = subprocess.Popen(["grep", "STUFF_", ".hgtags"], stdout=subprocess.PIPE)
    tag = p2.comminucate()[0].strip('\n')
    return tag

Here is what's really strange. This code works when you enter it line by line into interactive, but not when it's run thru a script. It also works when you import the script file into interactive and call the function, but not when it's called by the main function. The traceback I get from running the script straight is as follows:

Traceback (most recent call last):
File "./integration.py", line 64, in <module>
    main()  
File "./integration.py", line 48, in main
    tag = get_tag(folder)
File "./integration.py", line 9, in get_date
    os.chdir(directory)
OSError: [Errno 2] No such file or directory: ''

And it's called in the main function like this:

if block_dict[block][0]=='0':
    tag = get_tag(folder)

with "folder" being previously defined as a string.

Please note we use python 2.6 so I can't use the module check_output unfortunately.

share|improve this question

3 Answers 3

up vote 0 down vote accepted

Have you tried using the glob module as opposed to find?

import glob
glob.glob("/path/to/directory/*/SomeDir/path/*")

You can look past multiple dirctories using **:

glob.glob("/path/**/SomeDir/path/*")

and that would match /path/to/your/SomeDir/path/file.

share|improve this answer
    
How would that work if it's /path/blah/foldername.1234 and I only know the foldername part of that directory? –  user2506070 Jun 20 '13 at 17:27
    
** will match multiple directories, edited. –  Paul Rubel Jun 20 '13 at 17:50
    
This module perfectly replaces the find in my code, glob.glob("/path/to/dir/%s*" %part_of_foldername) expands to the correct path. Thanks kindly!!! –  user2506070 Jun 20 '13 at 21:29

evidently p1.communicate()[0].strip('\n') is returning an empty string. are you really using the hardcoded value "/path/to/directory" as in your example?

share|improve this answer
    
Yes, I was seeing it return the empty string, but it doesn't do that in interactive mode. –  user2506070 Jun 20 '13 at 17:26

Check the result of p1.communicate()[0]. It maybe empty string.

. in "%s.*" % part_of_foldername seems to be the cause.

UPDATE

Found typo: comminucate -> comminucate


def get_tag(part_of_foldername):
    p1 = subprocess.Popen(["find", "/path/to/directory", "-maxdepth", "1", "-name", "*%s*" % part_of_foldername, "-type", "d"], stdout=subprocess.PIPE)
    out, err = p1.communicate()
    directory = out.split('\n')[0]
    p1.wait()
    if directory:
        os.chdir(directory)
        p2 = subprocess.Popen(["grep", "STUFF_", ".hgtags"], stdout=subprocess.PIPE)
        out, err = p2.communicate()
        p2.wait()
        return out.rstrip('\n')
share|improve this answer
    
Unfortunately that typo is me failing at modifying the code from the original :( –  user2506070 Jun 20 '13 at 17:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.