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

G

globalrev

print os.path.exists('C:\Users\saftarn\Desktop\NetFlixDataSet
\trainingsetunzipped\training_set\mv_0000001.txt')

d=open('C:/Python25/myPrograms/mapexperiments/maps/provinces-of-
sweden.gif')
d.close()

exists returns false but when i open it doesnt complain. how come?

another file that exists returned false for complained when i tried to
open it.
 
T

Tim Golden

globalrev said:
print os.path.exists('C:\Users\saftarn\Desktop\NetFlixDataSet
\trainingsetunzipped\training_set\mv_0000001.txt')

d=open('C:/Python25/myPrograms/mapexperiments/maps/provinces-of-
sweden.gif')
d.close()

Ummm. These are not the same files (unless you've got some bizarre
NTFS linking going on).
exists returns false but when i open it doesnt complain. how come?

Also, in general, you're prone to race conditions if you do this
kind of thing, altho' on a local C: drive that's less likely. But
still possible. Probably better to wrap the open in a try-except
and handle an IOError.

TJG
 
R

Reedick, Andrew

-----Original Message-----
From: [email protected] [mailto:python-
[email protected]] On Behalf Of globalrev
Sent: Thursday, May 15, 2008 12:04 PM
To: (e-mail address removed)
Subject: exists=false, but no complaint when i open it!?

print os.path.exists('C:\Users\saftarn\Desktop\NetFlixDataSet
\trainingsetunzipped\training_set\mv_0000001.txt')

d=open('C:/Python25/myPrograms/mapexperiments/maps/provinces-of-
sweden.gif')
d.close()

exists returns false but when i open it doesnt complain. how come?

another file that exists returned false for complained when i tried to
open it.
--


You're falling victim to string interpolation because of the
backslashes. (\n == newline, \N == N).

Try using a raw string r'filename', instead of 'filename':
print
os.path.exists(r'C:\Users\saftarn\Desktop\NetFlixDataSet\trainingsetunzi
pped\training_set\mv_0000001.txt')


*****

The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. GA621
 
J

John Machin

print os.path.exists('C:\Users\saftarn\Desktop\NetFlixDataSet
\trainingsetunzipped\training_set\mv_0000001.txt')

d=open('C:/Python25/myPrograms/mapexperiments/maps/provinces-of-
sweden.gif')
d.close()

exists returns false but when i open it doesnt complain. how come?

another file that exists returned false for complained when i tried to
open it.

Ummm ... you are testing one path with os.path.exists and then opening
a *DIFFERENT* path. If you think you still have a problem, show us a
short piece of code that you actually executed and that demonstrates
the problem.
 
R

Reedick, Andrew

-----Original Message-----
From: [email protected] [mailto:python-
[email protected]] On Behalf Of Reedick, Andrew
Sent: Thursday, May 15, 2008 12:11 PM
To: globalrev; (e-mail address removed)
Subject: RE: exists=false, but no complaint when i open it!?
print os.path.exists('C:\Users\saftarn\Desktop\NetFlixDataSet
\trainingsetunzipped\training_set\mv_0000001.txt')


You're falling victim to string interpolation because of the
backslashes. (\n == newline, \N == N).

Try using a raw string r'filename', instead of 'filename':
print
os.path.exists(r'C:\Users\saftarn\Desktop\NetFlixDataSet\trainingsetunz
i
pped\training_set\mv_0000001.txt')

Specificially, the \t in '\trainingsetunzipped' and in '\training_set'
were being treated as tab characters. Ignore my comment about '\N ==
N'.
 
G

globalrev

yeah i was unclear but i tested different stuff at the same time.
and flim is not misspelled i just created it and jolted osmething down
and it became flim...

print os.path.exists('C:/Python25/myPrograms/netflix/flim.txt')
d=open('C:/Python25/myPrograms/netflix/flim.txt', 'r')
#print d.readline()
print d.read()

d.close() #not necessary?

print os.path.exists('C:\Users\saftarn\Desktop\NetFlixDataSet
\training_set')

i can read flim and exists returns true.

however
print os.path.exists('C:\Users\saftarn\Desktop\NetFlixDataSet')
returns True but
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.
 
G

globalrev

when i try to write to the file...

Traceback (most recent call last):
File "C:\Python25\myPrograms\netflix\netflix.py", line 10, in
<module>
d.write('hej du galne kock')
IOError: [Errno 0] Error
 
G

globalrev

when i try to write to the file...

Traceback (most recent call last):
File "C:\Python25\myPrograms\netflix\netflix.py", line 10, in
<module>
d.write('hej du galne kock')
IOError: [Errno 0] Error

damn i take a break lol.

r+ ldo when open file so i CAN write to it...
 
A

Andreas Tawn

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
 
S

s0suk3

when i try to write to the file...
Traceback (most recent call last):
File "C:\Python25\myPrograms\netflix\netflix.py", line 10, in
<module>
d.write('hej du galne kock')
IOError: [Errno 0] Error

damn i take a break lol.

r+ ldo when open file so i CAN write to it...

That traceback doesn't provide much help ("[Errno 0] Error"...), but I
remember that one time I got it when trying to write to a file opened
for reading. Make sure you're opening the file for writing (passing
"w" as the second argument to open(), or "wb" if you're writing binary
data). Also, as everyone keeps pointing out to you, watch out for
backslash scapes -- double the slashes ("\\", that really means a
single \ to Python), use forward slashes ("/", perhaps not the most
elegant solution, but it always works), or put an "r" next to the
string (r"your\path"). The last form is the easiest, but it only works
if you're typing a literal; you'll eventually need to double slashes
(e.g., for replacing something in a string: s.replace("/", "\\")).
 
H

Henrique Dante de Almeida

Em Thu, 15 May 2008 19:20:58 +0200, Andreas Tawn escreveu:
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
 

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

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,685
Members
48,796
Latest member
Greg L.

Latest Threads

Top