Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Check if a file exists.
#1
I need to look for a file in this path:

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"')
Reply


Messages In This Thread
Check if a file exists. - by Pedroski55 - Sep-08-2020, 05:58 AM
RE: Check if a file exists. - by bowlofred - Sep-08-2020, 06:32 AM
RE: Check if a file exists. - by buran - Sep-08-2020, 06:48 AM
RE: Check if a file exists. - by snippsat - Sep-08-2020, 09:38 AM
RE: Check if a file exists. - by perfringo - Sep-08-2020, 09:57 AM
RE: Check if a file exists. - by Pedroski55 - Sep-08-2020, 10:01 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  please check this i wanna use a csv file as a graph xCj11 5 1,516 Aug-25-2022, 08:19 PM
Last Post: deanhystad
  check if a file exist on the internet and get the size kucingkembar 6 1,823 Apr-16-2022, 05:09 PM
Last Post: kucingkembar
  Code to check folder and sub folders for new file and alert fioranosnake 2 1,964 Jan-06-2022, 05:03 PM
Last Post: deanhystad
  Check last time file was accessed Pavel_47 4 2,857 Jun-01-2021, 05:47 PM
Last Post: Yoriz
  How to check if a file has finished being written leocsmith 2 7,895 Apr-14-2021, 04:21 PM
Last Post: perfringo
  pathlib destpath.exists() true even file does not exist NaN 9 4,731 Dec-01-2020, 12:43 PM
Last Post: NaN
  How to check to see a dbf file is EOF ? DarkCoder2020 0 1,738 Jun-16-2020, 05:03 PM
Last Post: DarkCoder2020
  p]Why os.path.exists("abc/d") and os.path.exists("abc/D") treat same rajeev1729 1 2,187 May-27-2020, 08:34 AM
Last Post: DeaD_EyE
  Building a script to check size of file upon creation mightyn00b 2 2,400 Apr-04-2020, 04:39 AM
Last Post: Larz60+
  Shutil move if file exists in destination Friend 2 6,811 Feb-02-2020, 01:45 PM
Last Post: Friend

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020