Sep-08-2020, 05:58 AM
I need to look for a file in this path:
but sometimes this file
will not exist.
I think there must be some 'os.if_not_exists(file)' I can just return from the function.
What is the best way to check if the file exists?
Quote:pathToAnswersHW = '/home/pedro/' + termName[0] + '/' + theClass[0] + '/correctAnswersHW/'
but sometimes this file
Quote:theAnswersHW = open(pathToAnswersHW + 'correctAnswersHW' + theClass[0] + answersWeeknumber[0] + '.txt', 'r')
will not exist.
I think there must be some 'os.if_not_exists(file)' I can just return from the function.
What is the best way to check if the file exists?
def openAnswersHW(): pathToAnswersHW = '/home/pedro/' + termName[0] + '/' + theClass[0] + '/correctAnswersHW/' # HW get the correct HW answers with answerDataHW = theAnswersHW.readlines() theAnswersHW = open(pathToAnswersHW + 'correctAnswersHW' + theClass[0] + answersWeeknumber[0] + '.txt', 'r') # check if the file exists #put a return in here if the file does not exist answerDataHW = theAnswersHW.readlines() theAnswersHW.close() # HW standardize the apostrophes aposSorted = changeApostrophe(answerDataHW) echoMessage('Apostrophes sorted ... ') for word in aposSorted: answersHW.append(word) echoMessage('answersHW is: ' + str(len(answersHW)) + ' long.') echoMessage('Now click "tidy student answers CW" or "tidy student answers HW"')