所以我正在运行这个代码,它从文件夹中获取文件并将它们复制到另一个目录中。但是,当我尝试创建新目录树时,即使我没有在代码中的任何其他位置创建该目录树,也会出现FileExistsError。什么可能是错的?
import os
import shutil
import datetime
class CopyFilesModuleTest:
def __init__(self):
self.folderName = ""
self.timeName = CopyFilesModuleTest.formatTimeStr(datetime.datetime.now())
def getDirFiles(self):
src_files = os.listdir(self.folderName)
return src_files
def copyFiles(self,src_files):
for file_name in src_files:
full_file_name = self.folderName+"/"+file_name
if(os.path.isfile(full_file_name)):
destPath = self.getNewFileDest(file_name)
destDir = self.getNewFileDir()
self.createDirectory(destDir)
shutil.copy(full_file_name,destPath)
def getNewFileDest(self,fileName):
mainFold = "userDataBackUp"
full_File_Path = mainFold+"/"+self.timeName+"/"+self.folderName+"/"+fileName
return full_File_Path
def getNewFileDir(self):
mainFold = "userDataBackUp"
full_File_Dir = mainFold+"/"+self.timeName+"/"+self.folderName
return full_File_Dir
@staticmethod
def formatTimeStr(tStr):
tStr = str(tStr)
colon = ":"
space = " "
dot = "."
bLine = "-"
tStr = tStr.replace(colon,"_")
tStr = tStr.replace(space,"_")
tStr = tStr.replace(dot,"_")
tStr = tStr.replace(bLine,"_")
return tStr
def createDirectory(self,filePath):
os.makedirs(filePath)
def copyAllFromFolder(self,fName):
self.folderName = fName
src_files1 = cfm1.getDirFiles()
cfm1.copyFiles(src_files1)
cfm1 = CopyFilesModuleTest()
cfm1.copyAllFromFolder("folder_files")