checking for when a file or folder exists, typing problems?

B

Bryan Rasmussen

Hi
I have a little program that is importing from os.path import exists, join,
isdir, normpath, isfile
at one point in my program I check if a file exists using
if exists("c:\projects"):

and that works fine.

If I change it to be
if exists(thepath):
where thepath is a commandline argument it does not work fine.

Note that the commandline is c:\projects and when I print thepath to check what
is going on it prints
c:\projects

The only thing I can assume is that there is some sort of typing problem going
on here, but then it should go ahead and give an error then if it's getting
something unexpected obviously.

Any help on what this error is?

Thanks
 
B

Brian

Hi Bryan,

Here's a potential idea. Try converting the variable to a string by
using the following syntax:


thePath = str(thePathArg)


This will convert the current variable type to a string, which follows
the data type syntax that you have specified at the beginning of your
message.

Hope this helps,

Brian :)
 
S

Scott David Daniels

Bryan said:
... at one point in my program I check if a file exists using
if exists("c:\projects"):

You should not be using a backslash in non-raw-string source to
mean anything but "escape the next character." The above should
either be written as:
if exists(r"c:\projects"):
or:
if exists("c:\\projects"):

I suspect you problem has to do with this difference, but perhaps not.
Give exact short code that actually demonstrates the problem.

--Scott David Daniels
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top