Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 808
user42459 Avatar asked May 11 '15 18:05

user42459


People also ask

How do you rename the file if already exists in Python?

Python rename() file is a method used to rename a file or a directory in Python programming. The Python rename() file method can be declared by passing two arguments named src (Source) and dst (Destination).

How do you overwrite a filename in Python?

rename() method changes the name of a file. os. rename() accepts two arguments: the path of the old file and the path of the new file. The new file path should end in a different file name.

How do you rename a file in Python with the same name?

Use rename() method of an OS module Use the os. rename() method to rename a file in a folder. Pass both the old name and a new name to the os. rename(old_name, new_name) function to rename a file.

How do I change the name of an existing file?

Right-click the file and select Rename. Enter a new file name and press Enter.


2 Answers

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)
like image 95
sirfz Avatar answered Oct 04 '22 12:10

sirfz


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
like image 20
Padraic Cunningham Avatar answered Oct 04 '22 11:10

Padraic Cunningham