Force Overwrite in Os.Rename

后端 未结 7 1647
北荒
北荒 2020-11-30 05:10

Is it possible to force a rename os.rename to overwrite another file if it already exists? For example in the code below if the file Tests.csv already exists it would be re

7条回答
  •  伪装坚强ぢ
    2020-11-30 06:07

    How about use of replace() and shutil.move() ? example:

    import os
    from shutil import move
    
    filename=r'C:\Users\Test.txt'
    move(filename,filename.replace('.txt','.csv')
    

    also you can make it more general for all the files. Thanks

提交回复
热议问题