1

I'm trying to run a program in Python to run a module to process data from my smartwatch. I've had it working before but I've changed the filepath variable and now no matter what I try I can't get it to work.

I'm a mac user and when I try to run the script in terminal I get the following error:

Traceback (most recent call last):
  File "Descentlog_manual.py", line 53, in <module>
    fitfilelist = FitFileList(directory = '/Users/edwibberley/Garmin_descent_FIT_process/FarneIslands')
  File "Descentlog_manual.py", line 21, in __init__
    for fitfilename in os.listdir(self.directory):
FileNotFoundError: [Errno 2] No such file or directory: '/Users/edwibberley/Garmin_descent_FIT_process/FarneIslands'
(base) Edwards-MBP:FarneIslands  edwibberley$ python Descentlog_manual.py
/Users/edwibberley/Garmin_descent_FIT_process/FarneIslands 

From other answers on here most people seem to solve the problem by using an absolute filepath as opposed to a relative but I can't see how this is my issue. When I pwd directory I get the filepath as included in my code so I can't see where I'm going wrong.

My code from the script is below.

import os

class FitFile:
    def __init__(self,name,maxtime=2,nonumber=True,apnea=False):
        self.name = name
        self.maxtime = maxtime
        self.nonumber = nonumber
        self.apnea = apnea

class FitFileList:
    def __init__(self,directory):
        self.fitfilelist = []
        self.directory = directory
        for fitfilename in os.listdir(self.directory):
            if os.path.isfile(fitfilename) == True and fitfilename.endswith('.fit'): 
                self.fitfilelist.append(fitfilename)
                
        
def ProcessFitFiles(fitfilelist):
    for files in fitfilelist.fitfilelist:
        fit_file = FitFile(name = files)
        
        #currently setup to produce a seperate .xml file for each dive, minimum time threshold will be taken from above

        from fit2subsEW_module import settings

        settings.fit_files = [fit_file.name]
        settings.out_subslog = fit_file.name + '.xml'
        settings.min_time = fit_file.maxtime
        settings.no_numbering = fit_file.nonumber
        settings.apnea = fit_file.apnea

        print('fit file settings', settings.fit_files)
        print('fit file type',type(settings.fit_files))


        settings.check_settings()

        from fit2subsEW_module import start_processing

        start_processing()
        

fitfilelist = FitFileList(directory = '/Users/edwibberley/Garmin_descent_FIT_process/FarneIslands')

ProcessFitFiles(fitfilelist)

Any help would be greatly appreciated as I've been going round in circles on this and getting nowhere.

Edit - @Osmann Durdag

When I tried your second solution I got the following error message:

(base) Edwards-MacBook-Pro:FarneIslands edwibberley$ python Descentlog_manual.py Traceback (most recent call last): File "Descentlog_manual.py", line 54, in fitfilelist = FitFileList(directory = '/Users/edwibberley/Garmin_descent_FIT_process/FarneIslands') File "Descentlog_manual.py", line 23, in init for fitfilename in os.listdir(self.directory): FileNotFoundError: [Errno 2] No such file or directory: '/Users/edwibberley/Garmin_descent_FIT_process/FarneIslands' (base) Edwards-MacBook-Pro:FarneIslands edwibberley$ python Descentlog_manual.py /Users/edwibberley/Garmin_descent_FIT_process/FarneIslands Traceback (most recent call last): File "Descentlog_manual.py", line 54, in fitfilelist = FitFileList(directory = '/Users/edwibberley/Garmin_descent_FIT_process/FarneIslands') File "Descentlog_manual.py", line 22, in init print(os.listdir(self.directory)) FileNotFoundError: [Errno 2] No such file or directory: '/Users/edwibberley/Garmin_descent_FIT_process/FarneIslands' (base) Edwards-MacBook-Pro:FarneIslands edwibberley$

EdW
  • 11
  • 2
  • 1
    This may sound silly, but have you made sure `/Users/edwibberley/Garmin_descent_FIT_process/FarneIslands` exists and is readable? – Pranav Hosangadi Sep 25 '20 at 17:21
  • Try `/Users/edwibberley/Garmin_descent_FIT_process/FarneIslands/` please – Osman Durdag Sep 25 '20 at 17:22
  • Unrelated: you can use `glob` to get the filtered list directly instead of having to iterate through the `listdir` for `.fit` files. https://stackoverflow.com/questions/2225564/get-a-filtered-list-of-files-in-a-directory – Pranav Hosangadi Sep 25 '20 at 17:23
  • That's the directory where my .py script is saved and I checked it by dragging and dropping the folder it into my terminal window to get the correct format. I also tried a different directory as originally it was saved in my icloud which one user suggested could cause an issue. Is there anything else I can do to check it? – EdW Sep 25 '20 at 17:24
  • @OsmanDurdag I tried /Users/edwibberley/Garmin_descent_FIT_process/FarneIslands/ and unfortunately that doesnt work either. Still get the FileNotFoundError: [Errno 2] No such file or directory: '/Users/edwibberley/Garmin_descent_FIT_process/FarneIslands/' error. I've tried all the slight variations on filepath name I can think of – EdW Sep 25 '20 at 17:27
  • 1
    Usually there's no hidden dragons, it is just as it is .... your directory does not exist. – Raf Sep 25 '20 at 17:39
  • @Raf my directory does exist as its where the .py script is saved and the location of the files I'm trying to process. I don't know what else I can try to check it or fix it? – EdW Sep 30 '20 at 12:49
  • @EdW I didn't mean it literally. I meant the error message should be pointing towards the solution to the problem. If your script couldn't find a directory, it may be looking for it in a different place than you're thinking, or there may be some hidden/special chars, etc... debug it, run your algorithm in an interactive prompt and see if the same error happens – Raf Oct 02 '20 at 11:07

1 Answers1

0

In this for, fitfilename only file name, you must be added this file name to directory. I think this will help you

from os.path import join # import this

for fitfilename in os.listdir(self.directory):
            if os.path.isfile(join(self.directory, fitfilename)) == True and fitfilename.endswith('.fit'): 
                self.fitfilelist.append(fitfilename)

If you get any error again please do it and share again this print results:

class FitFileList:
    def __init__(self,directory):
        self.fitfilelist = []
        self.directory = directory
        print(self.directory)
        print(os.listdir(self.directory))
        for fitfilename in os.listdir(self.directory):
            if os.path.isfile(fitfilename) == True and fitfilename.endswith('.fit'): 
                self.fitfilelist.append(fitfilename)

Please try it:

fitfilelist = FitFileList(directory = r'/Users/edwibberley/Garmin_descent_FIT_process/FarneIslands')

or :

fitfilelist = FitFileList(directory = r'/Users/edwibberley/Garmin_descent_FIT_process/FarneIslans/')
Osman Durdag
  • 955
  • 1
  • 7
  • 18
  • Please share this print functions results. I added to FitFileList class them. – Osman Durdag Sep 25 '20 at 17:57
  • Still getting the same error, Thanks a lot for your help so far though – EdW Sep 25 '20 at 18:02
  • You are welcome. I updated my answer. Please check it – Osman Durdag Sep 25 '20 at 18:11
  • Tried the r'/.... path and still the same error message. fitfilelist = FitFileList(directory = r'/Users/edwibberley/Garmin_descent_FIT_process/FarneIslands/') File "Descentlog_manual.py", line 22, in __init__ print(os.listdir(self.directory)) FileNotFoundError: [Errno 2] No such file or directory: '/Users/edwibberley/Garmin_descent_FIT_process/FarneIslands/' – EdW Sep 26 '20 at 13:45