Search code examples
pythonregexfile-management

How can I test if a string refers to a file or directory? with regular expressions? in python?


so I'm writting a generic backup application with os module and pickle and far I've tried the code below to see if something is a file or directory (based on its string input and not its physical contents).

import os, re

def test(path):
    prog = re.compile("^[-\w,\s]+.[A-Za-z]{3}$")
    result = prog.match(path)
    if os.path.isfile(path) or result:
        print "is file"
    elif os.path.isdir(path):
        print "is directory"
    else: print "I dont know"

Problems

test("C:/treeOfFunFiles/")
is directory
test("/beach.jpg")
I dont know
test("beach.jpg")
I dont know
test("/directory/")
I dont know

Desired Output

test("C:/treeOfFunFiles/")
is directory
test("/beach.jpg")
is file
test("beach.jpg")
is file
test("/directory/")
is directory

Resources

what regular expression should I be using to tell the difference between what might be a file and what might be a directory? or is there a different way to go about this?


Solution

  • In a character class, if present and meant as a hyphen, the - needs to either be the first/last character, or escaped \- so change "^[\w-,\s]+\.[A-Za-z]{3}$" to "^[-\w,\s]+\.[A-Za-z]{3}$" for instance.

    Otherwise, I think using regex's to determine if something looks like a filename/directory is pointless...

    • /dev/fd0 isn't a file or directory for instance
    • ~/comm.pipe could look like a file but is a named pipe
    • ~/images/test is a symbolic link to a file called '~/images/holiday/photo1.jpg'

    Have a look at the os.path module which have functions that ask the OS what something is...: