重命名文件名的智能 python 程序
A smart python program that renames file names
所以,我正在制作一个 python 程序,将 A.txt 重命名为 B.txt,但如果有文件 "B.txt",我希望程序跳过并继续已经在文件夹中。同时,如果文件夹中既没有 A 也没有 B,则说明有问题,所以我希望程序显示错误并停止。
所以我想要"if A exists then rename it, if neither A nor B exists then show me error and stop the program, if only B exists then move on to next line"
我做的就是这个
import os
os.rename('A.txt','Btxt')
但是如果没有A.txt,程序就会停止并显示错误信息。我怎样才能编写我想要的代码?
这只会重命名 A.txt 如果它存在而 B.txt 不存在。
如果 A.txt 不存在,它会检查 B.txt 是否存在。
import os
if os.path.isfile("A.txt"):
if not os.path.isfile("B.txt"):
os.rename('A.txt','B.txt')
else:
assert os.path.isfile("B.txt") , "Neither A.txt or B.txt exists"
所以,我正在制作一个 python 程序,将 A.txt 重命名为 B.txt,但如果有文件 "B.txt",我希望程序跳过并继续已经在文件夹中。同时,如果文件夹中既没有 A 也没有 B,则说明有问题,所以我希望程序显示错误并停止。
所以我想要"if A exists then rename it, if neither A nor B exists then show me error and stop the program, if only B exists then move on to next line"
我做的就是这个
import os
os.rename('A.txt','Btxt')
但是如果没有A.txt,程序就会停止并显示错误信息。我怎样才能编写我想要的代码?
这只会重命名 A.txt 如果它存在而 B.txt 不存在。
如果 A.txt 不存在,它会检查 B.txt 是否存在。
import os
if os.path.isfile("A.txt"):
if not os.path.isfile("B.txt"):
os.rename('A.txt','B.txt')
else:
assert os.path.isfile("B.txt") , "Neither A.txt or B.txt exists"