How do you make Python distinguish from files and folders if they are dragged and dropped into the program's window?

import os
import string

newDrag = raw_input("Drag and drop the file or folder you want to add.\n")
newStrip = newDrag.strip('"')
#Removes the quotes Windows randomly adds for some reason

if string.find(newStrip, "."): 
#Checks for the period that precedes the file extension
    print "That is a file."
else:
#There is no period, no file extension, it is a folder.
    print "That is a folder."

os.system("pause")

I drop a new folder from Program Files into the program and...

Drag and drop the file or folder you want to add.
C:\Program Files\New Folder
That is a file.

Why doesn't this work right? It still prints "That is a file." even if nothing is typed in. It's just skipping the else: for some reason.

Recommended Answers

All 2 Replies

To test if a path is a file, you can use

import os
os.path.isfile(path) # is it a file ?
os.path.isdir(path) # is it a folder ?
os.path.exists(path) # does it exist in the file system ?

Thank you, that worked. I knew there had to be an easier way to do that. :$

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.