Message229936
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'
>>> |
|
Date |
User |
Action |
Args |
2014-10-24 16:00:12 | hosford42 | set | recipients:
+ hosford42, tim.golden, zach.ware, steve.dower |
2014-10-24 16:00:12 | hosford42 | set | messageid: <1414166412.24.0.659144032685.issue22719@psf.upfronthosting.co.za> |
2014-10-24 16:00:12 | hosford42 | link | issue22719 messages |
2014-10-24 16:00:11 | hosford42 | create | |
|