Python program to rename file names while overwriting if there already is that file
Asked Answered
L

5

19

As the title says, I wanted a python program that changes the file name, but I wanted to overwrite if there already is a file with that destination name.

import os, sys

original = sys.argv[1]
output = sys.argv[2]

os.rename(original, output)

But my code just shows me this error when there already is file with that destination name.

  os.rename<original, output>
WindowsError: [Error 183] Cannot create a file when that file already exists

What fix should I make?

Lamanna answered 11/5, 2015 at 18:49 Comment(1)
Remove the file and try again?Shepley
L
19

On Windows os.rename won't replace the destination file if it exists. You have to remove it first. You can catch the error and try again after removing the file:

import os

original = sys.argv[1]
output = sys.argv[2]

try:
    os.rename(original, output)
except WindowsError:
    os.remove(output)
    os.rename(original, output)
Linton answered 11/5, 2015 at 18:57 Comment(0)
P
9

You can use shutil.move, it will overwrite on windows:

from shutil import move

move(src,dest)

Demo:

In [10]: ls    
Directory of C:\Users\padraic\Desktop

11/05/2015  20:20    <DIR>          .
11/05/2015  20:20    <DIR>          ..
11/05/2015  20:20                 0 bar.txt
11/05/2015  20:20                 0 foo.txt
               2 File(s)              0 bytes
               2 Dir(s)  47,405,617,152 bytes free

In [11]: shutil.move("bar.txt","foo.txt")    
In [12]: ls 
Directory of C:\Users\padraic\Desktop    
11/05/2015  20:20    <DIR>          .
11/05/2015  20:20    <DIR>          ..
11/05/2015  20:20                 0 foo.txt
               1 File(s)              0 bytes
               2 Dir(s)  47,405,613,056 bytes free
In [13]: shutil.move("foo.txt","bar.txt")
In [14]: ls
 Volume in drive C has no label.
 Volume Serial Number is 3C67-52B9

 Directory of C:\Users\padraic\Desktop

11/05/2015  20:24    <DIR>          .
11/05/2015  20:24    <DIR>          ..
11/05/2015  20:20                 0 bar.txt
               1 File(s)              0 bytes
               2 Dir(s)  47,405,568,000 bytes free
Photomontage answered 11/5, 2015 at 19:0 Comment(2)
Thank you but does this really overwrite? It seems like it just ignores it when there is destination file..Lamanna
@user42459, it replaces the destination file, if you move a file with the same name you will seePhotomontage
C
0

This error only occurs on windows, as you can find in the python documentation ( https://docs.python.org/2/library/os.html#os.rename )

You should check if there is already a file or folder on the destination, with following code:

import os.path
os.path.exists(destination) 

See also this answer: https://mcmap.net/q/36054/-how-do-i-check-whether-a-file-exists-without-exceptions

If the file exists, remove it first before renaming the original file. Of course you should check if you are not removing the original file (so script.py file1 file1 should not remove file1).

Cooley answered 11/5, 2015 at 18:56 Comment(0)
U
0

Please find the below approach which i followed and it is working fine

source_file_name = 'Test.xlsx'
dst_file_name = "FinalName.xlsx"
source_file_path = "presentdirectory"  #os.getcwd()
dst_file_path = "Destination_Folderpath"
shutil.copy(os.path.join(source_file_path, source_file_name), os.path.join(dst_file_path,  dst_file_name))

It will overwrite the existing file with new data if it already exist also.

Utopianism answered 11/10, 2018 at 6:38 Comment(0)
P
0

os.rename() does not overwrite (atleast in Windows) if the destination file exits. So first check to see if the destination file exits and if it exists, delete it.

import os.path
# first check if file exists
if os.path.exists(outputFilename):
    os.remove(outputFilename) # file exits, delete it
# rename the file
os.rename(originalFilename, outputFilename)

Another option is to use shutil.move, it overwrites the destination file (atleast in Windows).

import shutil
shutil.move(originalFilename, outputFilename)

Although it is better to check first and remove (if exists) to avoid any potential error.

Prothalamion answered 1/5, 2020 at 11:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.