WindowsError: [Error 183] Cannot create a file when that file already exists in python

Viewed 2261

i tried with bellow mentioned code just to rename some files. but this same code works good in linux but not in windows. CODE

#!/usr/bin/env python
from os import listdir
import os
from os.path import isfile, join
mypath = 'D:\\Hadoo'
path = 'D:\\Hadoo'
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
Existing = []
repeated = []
for a in onlyfiles:
    f = a.index('}')
    b = a[f + 1:]
    Existing.append(b)
    os.rename(os.path.join(mypath, a), os.path.join(path, b))

InLinux input: file{}a1.txt , file{}a2.txt InLinux output: a1.txt , a2.txt

but same not happening in Windows . it through following error:

>python filer.py
Traceback (most recent call last):
File "filer.py", line 16, in <module>
os.rename(os.path.join(mypath, a), os.path.join(path, b))
WindowsError: [Error 183] Cannot create a file when that file already exists
0 Answers
Related