0

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.

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by WildBamaBoy
1

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 ?

Edited by Gribouillis: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.