Python program to rename file names while overwriting if there already is that file -
as title says, wanted python program changes file name, wanted overwrite if there file destination name.
import os, sys original = sys.argv[1] output = sys.argv[2] os.rename(original, output)
but code shows me error when there file destination name.
os.rename<original, output> windowserror: [error 183] cannot create file when file exists
what fix should make?
on windows os.rename
won't replace destination file if exists. have remove first. can catch error , try again after removing 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)
Comments
Post a Comment