我想创建一个可以帮助我复制类似jpg文件的代码。所以我知道文件是否存在对我很重要。
为此,我写下了以下代码:
from os import path
import shutil
oldfile = input("Which file do you want to copy? write that in numbers : ")
fadress = input("What is your file adress : ")
newfadress = input("Where do you want to copy your file : ")
f_num2 = oldfile.split('-')
for each_num in f_num2:
print(each_num)
newname = '_DSC{}.JPG'.format(each_num)
new2 = 'DSC_{}.JPG'.format(each_num)
if path.isfile(fadress + '\\' + newname):
shutil.copy(fadress + '\\' + newname, newfadress)
print(newname)
elif path.isfile(fadress + '\\' + new2):
shutil.copy(fadress + '\\' + new2, newfadress)
print(new2)
它在if语句之前工作。但似乎if语句没有获取os.path.isfile函数的输出。我该怎么做才能使这段代码工作?
答案 0 :(得分:1)
您可以在使用之前删除fadress
并使用path.join
加入路径吗?
path.isfile(path.join(fadress.strip(), newname))
(或)在阅读fadress
时将其剥离?
fadress = input("What is your file adress : ").strip()