exists=false, but no complaint when i open it!?

Henrique Dante de Almeida hdante at gmail.com
Thu May 15 19:42:24 EDT 2008


Em Thu, 15 May 2008 19:20:58 +0200, Andreas Tawn escreveu:

>>print os.path.exists('C:\Users\saftarn\Desktop\NetFlixDataSet
>>\training_set') returns False...
>>
>>i have thourogly checked the filename to be correct and if we assume it
>>is what could this mean then?
>>i had a problem one other time when i had renamed a file but windows
>>didnt rename it compeltely apparently.
> 
> It's escape characters again.
> 
> You're asking for
> 'C:\Users\saftarn\Desktop\NetFlixDataSet\training_set', but Python
> interprets that as 'C:\Users\saftarn\Desktop\NetFlixDataSet
> raining_set', which probably doesn't exist.
> 
> The first example works by accident because backslash plus the first
> letter of each folder in your path happens to not match any of Python's
> string formatting characters.
> 
> Use forward slashes or double up the backslashes to stop this happening.
> 
> Cheers,
> 
> Drea

 Hint: standardize the way you write paths in python:

import os.path
def _p(*x):
    return os.path.normpath(os.path.expanduser(os.path.join(*x)))

(in my system)

In [10]: print _p('abcd.txt')
abcd.txt

In [11]: print _p('~/abcd.txt')
/home/hdante/abcd.txt

In [12]: print _p('~/abcd/efgh.txt')
/home/hdante/abcd/efgh.txt

In [13]: print _p('~', 'abcd', 'efgh.txt')
/home/hdante/abcd/efgh.txt




More information about the Python-list mailing list