Force Overwrite in Os.Rename

后端 未结 7 1644
北荒
北荒 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 05:59

    You could try shutil.move():

    from shutil import move
    
    move('C:\\Users\\Test.txt', 'C:\\Users\\Tests.csv')
    

    Or os.remove and then shutil.move:

    from os import remove
    from shutil import move
    
    remove('C:\\Users\\Tests.csv')
    move('C:\\Users\\Test.txt', 'C:\\Users\\Tests.csv')
    

提交回复
热议问题