473,386 Members | 1,962 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

print os.path.exists('C:\Users\saftarn\Desktop\NetFlixDa taSet
\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.
Jun 27 '08 #1
10 1666
globalrev wrote:
print os.path.exists('C:\Users\saftarn\Desktop\NetFlixDa taSet
\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
Jun 27 '08 #2
-----Original Message-----
From: py********************************@python.org [mailto:python-
li*************************@python.org] On Behalf Of globalrev
Sent: Thursday, May 15, 2008 12:04 PM
To: py*********@python.org
Subject: exists=false, but no complaint when i open it!?

print os.path.exists('C:\Users\saftarn\Desktop\NetFlixDa taSet
\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\NetFlixD ataSet\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
Jun 27 '08 #3
On May 16, 2:03 am, globalrev <skanem...@yahoo.sewrote:
print os.path.exists('C:\Users\saftarn\Desktop\NetFlixDa taSet
\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.
Jun 27 '08 #4

-----Original Message-----
From: py********************************@python.org [mailto:python-
li*************************@python.org] On Behalf Of Reedick, Andrew
Sent: Thursday, May 15, 2008 12:11 PM
To: globalrev; py*********@python.org
Subject: RE: exists=false, but no complaint when i open it!?

print os.path.exists('C:\Users\saftarn\Desktop\NetFlixDa taSet
\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\NetFlixD ataSet\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'.
Jun 27 '08 #5
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\NetFlixDa taSet
\training_set')

i can read flim and exists returns true.

however
print os.path.exists('C:\Users\saftarn\Desktop\NetFlixDa taSet')
returns True but
print os.path.exists('C:\Users\saftarn\Desktop\NetFlixDa taSet
\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.
Jun 27 '08 #6
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
Jun 27 '08 #7
On 15 Maj, 19:04, globalrev <skanem...@yahoo.sewrote:
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...
Jun 27 '08 #8
>print os.path.exists('C:\Users\saftarn\Desktop\NetFlixDa taSet
>\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
Jun 27 '08 #9
On May 15, 12:07 pm, globalrev <skanem...@yahoo.sewrote:
On 15 Maj, 19:04, globalrev <skanem...@yahoo.sewrote:
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("/", "\\")).

Jun 27 '08 #10
Em Thu, 15 May 2008 19:20:58 +0200, Andreas Tawn escreveu:
>>print os.path.exists('C:\Users\saftarn\Desktop\NetFlixDa taSet
\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

Jun 27 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jason Charalambides | last post by:
I set a program to automatically load values from a temporary file. However, there is a chance that the specific temporary file "C:\Temp\TU.tmp" may not exist at all. In that case I want that...
2
by: Alex | last post by:
Hi Everybody, I am new to Oracle. I just installed the Oracle9i (personal edition) on my Window XP system. I am trying to simply run some basic SQL commands using SQLPlus. After entering...
15
by: Geiregat Jonas | last post by:
is using if(open("file",O_EXCL) != -1){ printf("File does exists")}else{printf("file does not exists"); } a good way of checking if a file exists or not, if not how should I do it ?
0
by: ITALstudio s.r.l. | last post by:
Hy, In my application I create instances of windows forms into different AppDomain. If I repeat 5 times the following sequence: - Creating AppDomain; - Creating instance of windows form...
7
by: sprash | last post by:
Newbie question: I'm trying to determine if a file physically exists regardless of the permissions on it Using File.Exists() returns false if it physically exists but the process does not...
2
by: Anasi | last post by:
Hi, I have a problem with a rule that is updating a oracle 10 database. The rule is expected to check if ther is a invoice number then take the highest of them, and if there isnt any, then choose...
1
by: HemantSa | last post by:
Session exists when i close browser in firefox...is there any way to remove the session if i open the new browser instance.... Regards, Hemant Sandbhor
10
by: sitko | last post by:
Hi, I'm a VB programmer, and was wondering, is there a way to check and see if a file exists in straight C? In VB the code is: File.Exists(strDir & "filename.txt") 'returns a boolean true if...
7
by: Cirene | last post by:
I installed the RTM version of Visual Studio 2008 today. Initially when I would open any of my 2005 projects (.NET 2.0) it would simply open them. Now the Visual Studio Conversion Wizard is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.