简体   繁体   English

os.rename 无法创建已存在的文件

[英]os.rename cannot create a file that already exists

Now i am trying to rename the contents of a folder that contains txt files using python:现在我正在尝试使用 python 重命名包含 txt 文件的文件夹的内容:

The original files naming starts from 0.txt to 100.txt原始文件命名从 0.txt 到 100.txt

and i want to change their name to start from 10 instead of 0 (so the files would be 10.txt to 110.txt for example)我想将它们的名称更改为从 10 而不是 0 开始(例如,文件将是 10.txt 到 110.txt)

I have 2 lists, one contains the original names path and another one with the new names path,and i am trying to use os.rename() or shutil.move() to rename the files.我有 2 个列表,一个包含原始名称路径,另一个包含新名称路径,我正在尝试使用 os.rename() 或 shutil.move() 来重命名文件。

However, when i try using os.rename(), i get the error that i cannot create a file that already exists.但是,当我尝试使用 os.rename() 时,出现无法创建已存在文件的错误。

When i try to use shutil.move(), it deletes every single repeated folder, giving me only the last 9 folders(101 to 110).当我尝试使用 shutil.move() 时,它会删除每个重复的文件夹,只给我最后 9 个文件夹(101 到 110)。

Is there a way around this ??有没有解决的办法 ??

I'd advise to move the files and rename them in a temporary directory, delete the original files and move the files back from the temporary directory.我建议移动文件并在临时目录中重命名它们,删除原始文件并将文件从临时目录移回。 And then you can optionally delete the temporary directory you created.然后您可以选择删除您创建的临时目录。

You accurately defined the problem.你准确地定义了问题。 For instance, when you deal with 02.txt, you rename it to 12.txt.例如,当您处理 02.txt 时,您将其重命名为 12.txt。 Ten iterations later, you rename the same file to 22.txt, then 23.txt, ... until you finish with 102.txt.十次迭代后,您将同一个文件重命名为 22.txt,然后是 23.txt,...直到完成 102.txt。 You do this with the first ten files, systematically wiping out your other 90 files.您对前 10 个文件执行此操作,系统地清除其他 90 个文件。

You have to start at the end of the name space where you have expansion room.您必须从具有扩展空间的名称空间的末尾开始。 Work from 100.txt downward .从 100.txt向下工作。 Rename 100.txt to 110.txt, then 99.txt to 109.txt, etc. This way, you always have an interval of 10 unused file names for your movements.将 100.txt 重命名为 110.txt,然后将 99.txt 重命名为 109.txt,等等。这样,您的动作总是有 10 个未使用的文件名的间隔。

Coding is left as an exercise for the reader.编码留给读者作为练习。 :-) :-)

It sounds like you need to start at the end of your list.听起来您需要从列表的末尾开始。 Try moving 100->110 first, then 99->109, etc. Since 11 is going to already exist when you try moving 1->11 if you start at the beginning of your lists.尝试先移动 100->110,然后移动 99->109,等等。因为如果您从列表的开头开始移动 1->11,那么当您尝试移动 1->11 时,11 将已经存在。

EDIT: I'd actually recommend Tarranoth's answer above编辑:我实际上推荐上面的 Tarranoth 的答案

use os.replace() to replace a file that already exists.使用os.replace()替换已经存在的文件。 To check if file exists, use os.path.isfile('file path')要检查文件是否存在,请使用os.path.isfile('file path')

Try to reverse the lists (with original names and new names):尝试反转列表(使用原始名称和新名称):

listOrig = ["0.txt", "1.txt", ..., "100.txt"]
listNew = ["10.txt", "11.txt", ..., "110.txt"]

# Reverse the lists
listOrig = listOrig.reverse() # ["100.txt" ... "0.txt"]
listNew = listNew.reverse() # ["110.txt" ... "10.txt"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM