Python-重命名特定文件

Python-重命名特定文件,python,operating-system,rename,python-3.7,filenames,Python,Operating System,Rename,Python 3.7,Filenames,我需要能够重命名一个文件,不断增加文件末尾的数字,而不删除以前版本的文件。最初的思维过程是 file_path1 = "path/to/file/OLDqueues.sqlite" try: os.remove(file_path1) except OSError as e: print("Error: %s : %s" % (file_path1, e.strerror)) try: os.rename(r'path\to\fi

我需要能够重命名一个文件,不断增加文件末尾的数字,而不删除以前版本的文件。最初的思维过程是

file_path1 = "path/to/file/OLDqueues.sqlite"

try:
    os.remove(file_path1)
except OSError as e:
    print("Error: %s : %s" % (file_path1, e.strerror))

try:
   os.rename(r'path\to\file\queues.sqlite', r'path\to\file\OLDqueues.sqlite')
except OSError as e:
    print("Error: %s : %s" % (file_path1, e.strerror))
但是,我被告知在此过程中无法删除OLDqueues文件。将文件重命名为OLDqueues的最佳方法是什么,但如果OLDqueues已经存在,它将自动重命名为OLDqueues(1),然后是OLDqueues(2),以此类推


作为参考,我使用的是Python 3.7,这将应用于位于不同位置的两个文件,但不得影响目录中的任何其他文件。

如果您不想删除旧文件,只需保存增量版本,我不确定您为什么要删除该文件

这是我过去实施的方式:

if os.path.exists(file_path1):
    file_path2 = ## insert file name increment logic here
    # copy file as described in the link below using shutil

最好的方法是检查OLDqueues是否已经存在,如果已经存在,则检查OLDqueues(1)是否存在,等等,直到找到一个尚不存在的文件名,然后使用该文件名。如果这都在同一个线程中,则可以使用局部变量作为缓存,否则,将使用“OLDqueues”全局循环,并为每个现有文件递增名称,直到完成,然后以递增+=1的方式重命名。这是否回答了您的问题?