roundcrisis
roundcrisis

Reputation: 17806

os.path.isdir not returning True when given paths to actual existing directories

I have the following code:

path = os.path.join(svn_drive, svn_repo_path, relative_path)
if os.path.isdir(path.encode('string-escape')):             
            print path, " is  a directory"
else:               
            print path, " is not a directory"

this results in the following:

D:\mysvn\trunk\Assets\myfile.max  is not a directory    
D:\mysvn\\Animations  is not a directory
....

i.e. the problem is that os.path.isdir doesn't seems to recognize the fact that the path is actually a directory

svn_drive is the drive letter in this case D: svn_repo_path is in this case mysvn relative path is a path relative to the svn repo (that I obtained by parsing the results of svn log)

I have tried escaping, not escaping, many os.path methods (abspath, basename, etc) and nothing seems to work :(

I also accept alternatives ;), I just want to be able to know a path and then email the files, don't really mind how (I know sometimes people want to keep their code, but this is just a stand alone script)

I also need to open the file at a later stage to email it and I get a file not found that I am going to guess starts here

Full function listing (in case it helps):

def parse_svn_results(lines, svn_drive, svn_repo_path): 
    result = []
    for x in lines.split("\n"):
        if  "trunk/" in x:
            relative_path = x.lstrip('MDA ').replace("/","",1).replace("/", os.sep)
            path = os.path.join(svn_drive, svn_repo_path, relative_path)
            if os.path.isdir(path.encode('string-escape')):             
                print path, " is  a directory"
            else:               
                print path, " is not a directory"
                result.append(path)
    return result

UPDATE

this is a workaround version of the code but still I can't do imghdr.what(filename) (where filename is one of the files in result)

def parse_svn_results(lines, svn_drive, svn_repo_path):
    result = []
    for x in lines.split("\n"):
        if "trunk/" in x:
            relative_path = x.lstrip('MDA ').replace("/", "", 1).replace("/", os.sep)
            temp_path = os.path.join(svn_drive, os.sep, svn_repo_path, relative_path)
            path = format_path(temp_path)
            if path is not None:
                result.append(path)
    return result


def format_path(file_destination):
    file_name = os.path.basename(file_destination)
    path = os.path.dirname(file_destination)
    base, ext = os.path.splitext(file_name)
    picture_format = None
    e = ext if picture_format is None else '.%s' % picture_format.lower()
    if e:
        to_path = os.path.join(path, base + e)
        return to_path

Upvotes: 2

Views: 2850

Answers (3)

roundcrisis
roundcrisis

Reputation: 17806

Ok, the solution came from another question, I had to strip the \r at the end , I was sure that was being added by path.join but nope python filename on windows

Upvotes: 0

Bleeding Fingers
Bleeding Fingers

Reputation: 7129

I don't see a point in using the encode but if it's an absolute must then I suggest you further condition it's result by passing it through os.path.normpath and then throw it onto os.path.isdir

Upvotes: 1

Matti John
Matti John

Reputation: 20527

I think svn_drive should be D:/ not D:. See this line in the documentation:

Note that on Windows, since there is a current directory for each drive, os.path.join("c:", "foo") represents a path relative to the current directory on drive C: (c:foo), not c:\foo.

Upvotes: 1

Related Questions