[Tutor] os.rename error problem
Kent Johnson
kent_johnson at skillsoft.com
Tue Nov 2 11:43:29 CET 2004
Justin,
Is the old file open? Maybe you changed the ID tag and didn't close the file?
PS a few stylistic notes:
You don't need to compare to True and False in your conditionals:
> if new.lower().endswith('.mp3') == False:
could be
if not new.lower().endswith('.mp3'):
and
> if os.path.isfile(item + '/' + old) == True:
could be
if os.path.isfile(item + '/' + old):
Actually these are not quite equivalent - any Python value can be used in a
conditional, not just True and False. Generally this is the behavior you want:
>>> if 2: print 'True'
...
True
>>> if 2 == True: print 'True'
...
>>> if not []: print 'False'
...
False
>>> if [] == False: print 'False'
...
>>>
> else: pass
You don't need this empty else at all. Maybe you left it in as a
placeholder, that's fine, but in general if the else clause is empty you
can just leave it out.
Kent
At 11:17 PM 11/1/2004 -0800, justin wrote:
>Hello,
>
>I know os.rename is pretty easy and straight forward to use but Im trippin
>up on
>something here. In the interactive prompt I have no problem renaming a file.
>Even the file used below. But in a project im working on Im having a some
>problems.
>
>#### Start Code
>
>def reName():
>
> new = fileEntry.get() # Entry widget with the file name
> if new.lower().endswith('.mp3') == False:
> new = new + '.mp3'
>
> old = main_display.get(ACTIVE) # gets the orig file name from the
> listbox
>
> for item in pathList: # Adds the file names to the path
> if os.path.isfile(item + '/' + old) == True:
> old = item + '/' + old
> new = item + '/' + new
> else: pass
>
> print 'Source Exits? ' + str(os.path.isfile(old))
> print old
> print 'Destination Exists? ' + str(os.path.isfile(new))
> print new
> os.rename(old, new)
>
>#### End Code
>
> >>>
>Source Exits? True
>E:/Phosphorescent/Phosphorescent - ocean_of_storms.mp3
>Destination Exists? False
>E:/Phosphorescent/Phosphorescent - Ocean of Storms.mp3
>Exception in Tkinter callback
>Traceback (most recent call last):
> File "D:\PYTHON23\lib\lib-tk\Tkinter.py", line 1345, in __call__
> return self.func(*args)
> File "E:\work\Python\ID3edit\v2.0\ID3edit.py", line 71, in reName
> os.rename(old, new)
>OSError: [Errno 13] Permission denied
> >>>
>
>Im using Win2k and have checked to see if the file is read-only. Its not and I
>have been able to edit the MP3 ID3 tag information without problem.
>
>Ive read the bit in the 2.3 reference about OSError being raised if the
>destination exists, but os.path.isfile(new) says False.
>
>Can anyone give me a hint as to what Im missing here?
>
>Regards,
>Justin
>
>_______________________________________________
>Tutor maillist - Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list