-1

I'm trying to renaming some files in a directory using os module but giving an error

Traceback (most recent call last): File "rename.py", line 32, in Rename() File "rename.py", line 28, in Rename os.rename((final_directory+'/'+file), (final_directory+'/'+file.replace('.csv', '_1m.csv'))) WindowsError: [Error 183] Cannot create a file when that file already exists

i'm trying to rename 8 csv file with unique name, i see that the first if statement returning the error, but when i remove other 7 elif statement this works!

any help would be appreciated!

here is the file source file names

india_2018-10-25 (1).csv
india_2018-10-25 (2).csv
india_2018-10-25 (3).csv
india_2018-10-25 (4).csv
india_2018-10-25 (5).csv
india_2018-10-25 (6).csv
india_2018-10-25 (7).csv
india_2018-10-25.csv

here is the code

import os

def Rename():
    current_directory = os.getcwd()
    final_directory = os.path.join(current_directory, r'data')
    if not os.path.exists(final_directory):
        os.makedirs(final_directory)

    for file in os.listdir(final_directory):
        #print(file)
        if "(" not in file:
            os.rename((final_directory+'/'+file), (final_directory+'/'+file.replace('.csv', '_1m.csv')))
        elif file.endswith('(1).csv'):
            os.rename((final_directory+'/'+file), (final_directory+'/'+file.replace(' (1)', '_5m')))
        elif file.endswith('(2).csv'):
            os.rename((final_directory+'/'+file), (final_directory+'/'+file.replace(' (2)', '_15m')))
        elif file.endswith('(3).csv'):
            os.rename((final_directory+'/'+file), (final_directory+'/'+file.replace(' (3)', '_1h')))
        elif file.endswith('(4).csv'):
            os.rename((final_directory+'/'+file), (final_directory+'/'+file.replace(' (4)', '_4h')))
        elif file.endswith('(5).csv'):
            os.rename((final_directory+'/'+file), (final_directory+'/'+file.replace(' (5)', '_1D')))
        elif file.endswith('(6).csv'):
            os.rename((final_directory+'/'+file), (final_directory+'/'+file.replace(' (6)', '_1W')))
        elif file.endswith('(7).csv'):
            os.rename((final_directory+'/'+file), (final_directory+'/'+file.replace(' (7)', '_1M')))
        else:
            pass

Rename()
Sohan Das
  • 1,560
  • 2
  • 15
  • 16

2 Answers2

1

The problem is Windows OS is not case sensitive.

When your program loops through india_2018-10-25 (7).csv it already renamed the file to india_2018-10-25_1M.csv. When you try to save the file india_2018-10-25.csv as india_2018-10-25_1m.csv, because it already exists, os returns an error.

Solution: ensure your renaming conditions are unique on a case sensitive basis.

Also, it's best to follow DRY practice and simplify your code:

# define a translation table for the renaming schema
rename_dict = {
    ' (1)': '_5m',
    ' (2)': '_15m',
    ' (3)': '_1h',
    ' (4)': '_4h',
    ' (5)': '_1D',
    ' (6)': '_1W',
    ' (7)': '_1N',
}

for file in os.listdir(final_directory):
        fullpath = os.path.join(final_directory,file)
        #print(file)
        if "(" not in file:
            os.rename(fullpath, os.path.join(final_directory, file.replace('.csv', '_1m.csv')))
        else
            cond = os.path.splitext(file)[0][-4]
            file.replace(cond, rename_dict.get(cond))
            os.rename(fullpath, os.path.join(final_directory, file)
r.ook
  • 13,466
  • 2
  • 22
  • 39
1

You are trying to rename file to a name which is already there in the folder. make sure you are looking 1st on the india_2018-10-25.csv and not on india_2018-10-25 (7).csv. Find a way to figure what is the original cav name

fileList = os.listdir(path)
orgName = fileList[0].split(' (')[0]
if not orgName.endswith('.csv'):
    orgName += '.csv'
for i in range(1, 8):
    newName = orgName.replace('.csv', ' {%s}.csv'%i)
    if CheckFilaName(newName):  # check if exist and replace
        break
user1889297
  • 464
  • 5
  • 13