[New-bugs-announce] [issue22719] os.path.isfile & os.path.exists but in while loop

Aaron report at bugs.python.org
Fri Oct 24 18:00:12 CEST 2014


New submission from Aaron:

When using os.path.isfile() and os.path.exists() in a while loop under certain conditions, os.path.isfile() returns True for paths that do not actually exist.

Conditions:
The folder "C:\Users\EAARHOS\Desktop\Python Review" exists, as do the files "C:\Users\EAARHOS\Desktop\Python Review\baseExcel.py" and "C:\Users\EAARHOS\Desktop\Python Review\baseExcel.py.bak". (Note that I also tested this on a path that contained no spaces, and got the same results.)

Code:
>>> bak_path = r"C:\Users\EAARHOS\Desktop\Python Review\baseExcel.py"
>>> while os.path.isfile(bak_path):
...     bak_path += '.bak'
...     if not os.path.isfile(bak_path):
...         break
Traceback (most recent call last):
  File "<interactive input>", line 3, in <module>
  File "C:\Installs\Python33\Lib\genericpath.py", line 29, in isfile
    st = os.stat(path)
ValueError: path too long for Windows
>>> os.path.isfile(r"C:\Users\EAARHOS\Desktop\Python Review\baseExcel.py.bak.bak")
False
>>> 

>>> bak_path = r"C:\Users\EAARHOS\Desktop\Python Review\baseExcel.py"
>>> while os.path.exists(bak_path):
...     bak_path += '.bak'
...     if not os.path.exists(bak_path):
...         break
Traceback (most recent call last):
  File "<interactive input>", line 3, in <module>
  File "C:\Installs\Python33\Lib\genericpath.py", line 18, in exists
    st = os.stat(path)
ValueError: path too long for Windows
>>> os.path.exists(r"C:\Users\EAARHOS\Desktop\Python Review\baseExcel.py.bak.bak")
False
>>> 

>>> bak_path = r"C:\Users\EAARHOS\Desktop\Python Review\baseExcel.py"
>>> os.path.isfile(bak_path), os.path.exists(bak_path)
(True, True)
>>> bak_path += '.bak'
>>> os.path.isfile(bak_path), os.path.exists(bak_path)
(True, True)
>>> bak_path += '.bak'
>>> os.path.isfile(bak_path), os.path.exists(bak_path)
(True, True)
>>> bak_path
'C:\\Users\\EAARHOS\\Desktop\\Python Review\\baseExcel.py.bak.bak'
>>> temp = bak_path
>>> os.path.isfile(temp), os.path.exists(temp)
(True, True)
>>> os.path.isfile('C:\\Users\\EAARHOS\\Desktop\\Python Review\\baseExcel.py.bak.bak'), os.path.exists('C:\\Users\\EAARHOS\\Desktop\\Python Review\\baseExcel.py.bak.bak')
(False, False)
>>> 

On the other hand, this code works as expected:

>>> bak_path = r"C:\Users\EAARHOS\Desktop\Python Review\baseExcel.py"
>>> while os.path.isfile(bak_path):
...     temp = bak_path + '.bak'
...     bak_path = temp
... 
>>> bak_path
'C:\\Users\\EAARHOS\\Desktop\\Python Review\\baseExcel.py.bak.bak'
>>> 

>>> bak_path = r"C:\Users\EAARHOS\Desktop\Python Review\baseExcel.py"
>>> while os.path.exists(bak_path):
...     temp = bak_path + '.bak'
...     bak_path = temp
... 
>>> bak_path
'C:\\Users\\EAARHOS\\Desktop\\Python Review\\baseExcel.py.bak.bak'
>>>

----------
components: Windows
messages: 229936
nosy: hosford42, steve.dower, tim.golden, zach.ware
priority: normal
severity: normal
status: open
title: os.path.isfile & os.path.exists but in while loop
type: behavior
versions: Python 3.3

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue22719>
_______________________________________


More information about the New-bugs-announce mailing list