Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > python os.path.exists failure

Reply
Thread Tools

python os.path.exists failure

 
 
koranthala
Guest
Posts: n/a
 
      10-31-2009
Hi all,
My code is as follows:

path = r'C:/"Program Files"/testfolder/2.3/test.txt'
if os.path.lexists(path):
print 'Path Exists'
else:
print 'No file found in path - %s' %path
print Popen(path, stdout=PIPE, shell=True).stdout.read()

The output comes as
No file found in path - C:/"Program Files"/testfolder/2.3/test.txt
but the test.txt file is opened.

The issue, I guess, is that the double quotes inside is failing the
check. But without the double quotes, Popen fails.
One solution, I can think is to check without double quotes, and then
using some code, put the double quotes back inside, but it looks quite
kludgy.

What is the usual solution to this?
 
Reply With Quote
 
 
 
 
Benjamin Kaplan
Guest
Posts: n/a
 
      10-31-2009
On Sat, Oct 31, 2009 at 11:26 AM, koranthala <> wrote:
> Hi all,
> * My code is as follows:
>
> path = r'C:/"Program Files"/testfolder/2.3/test.txt'
> if os.path.lexists(path):
> * *print 'Path Exists'
> else:
> * *print 'No file found in path - %s' %path
> print Popen(path, stdout=PIPE, shell=True).stdout.read()
>
> The output comes as
> No file found in path - C:/"Program Files"/testfolder/2.3/test.txt
> but the test.txt file is opened.
>
> The issue, I guess, is that the double quotes inside is failing the
> check. But without the double quotes, Popen fails.
> One solution, I can think is to check without double quotes, and then
> using some code, put the double quotes back inside, but it looks quite
> kludgy.
>


Just out of curiosity, does 'C:/"Program FIles"/' even work on the
Windows command line? The usual procedure is to put the entire path in
quotes. r'"C:\Program Files\..."'.


> What is the usual solution to this?
> --
> http://mail.python.org/mailman/listinfo/python-list
>

 
Reply With Quote
 
 
 
 
Roel Schroeven
Guest
Posts: n/a
 
      11-01-2009
koranthala schreef:
> Hi all,
> My code is as follows:
>
> path = r'C:/"Program Files"/testfolder/2.3/test.txt'
> if os.path.lexists(path):
> print 'Path Exists'
> else:
> print 'No file found in path - %s' %path
> print Popen(path, stdout=PIPE, shell=True).stdout.read()
>
> The output comes as
> No file found in path - C:/"Program Files"/testfolder/2.3/test.txt
> but the test.txt file is opened.
>
> The issue, I guess, is that the double quotes inside is failing the
> check. But without the double quotes, Popen fails.
> One solution, I can think is to check without double quotes, and then
> using some code, put the double quotes back inside, but it looks quite
> kludgy.


You can put the double quotes around the whole path instead of just
around "Program Files" in the call to Popen().

The issue here is actually that os.path.exists() (and all other Python
functions) use the path exactly like you pass it; but with your call to
Popen(), the path is passed as an argument to the shell, and it needs to
be quoted for the shell to interpret that correctly.

So here's what I would do: first specify the path literally without
quotes, and quote it only when needed:

path = r'C:/Program Files/testfolder/2.3/test.txt'
if os.path.lexists(path):
print 'Path Exists'
else:
print 'No file found in path - %s' %path
print Popen('"%s"' % path, stdout=PIPE, shell=True).stdout.read()

Some other notes:
- Since you use forward slashes, there's no need to use a raw string
literal.
- You can just as well use os.path.exists() instead of
os.path.lexists(). The difference has to do with symbolic links, which
Windows doesn't have.
- I'm not sure what you expect the line with Popen to do. On my system,
it opens the specified text file in notepad and returns an empty string.
If that's what you want to do, it's easier with os.startfile().

--
The saddest aspect of life right now is that science gathers knowledge
faster than society gathers wisdom.
-- Isaac Asimov

Roel Schroeven
 
Reply With Quote
 
Aahz
Guest
Posts: n/a
 
      11-04-2009
In article <9aaf6a31-a34e-454b-a8f0->,
koranthala <> wrote:
>
>path = r'C:/"Program Files"/testfolder/2.3/test.txt'
>if os.path.lexists(path):
> print 'Path Exists'
>else:
> print 'No file found in path - %s' %path
>print Popen(path, stdout=PIPE, shell=True).stdout.read()


Avoiding shell=True is a Good Idea
--
Aahz () <*> http://www.pythoncraft.com/

[on old computer technologies and programmers] "Fancy tail fins on a
brand new '59 Cadillac didn't mean throwing out a whole generation of
mechanics who started with model As." --Andrew Dalke
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Success Or Failure: There Is No Such Thing As Failure bs866806@163.com C Programming 0 01-06-2008 11:41 AM
[EVALUATION] - E01: The Java Failure - May Python Helps? Ilias Lazaridis Python 24 02-13-2005 10:54 AM
[JAVA] [EVALUATION] - The Java Failure (Sorry: The Java(tm) Failure) Ilias Lazaridis Java 0 02-01-2005 10:32 AM
Building Python 2.3 on HP-UX 10.20 - pthread failure Dan Cescato Python 9 10-06-2003 01:40 AM
RE: Building Python 2.3 on HP-UX 10.20 - pthread failure Tim Peters Python 0 10-03-2003 09:44 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57