【发布时间】:2021-01-12 01:23:50
【问题描述】:
我正在尝试在 Python 中运行一个程序来运行一个模块来处理来自我的智能手表的数据。我之前已经让它工作了,但我已经更改了文件路径变量,现在无论我尝试什么,我都无法让它工作。
我是 mac 用户,当我尝试在终端中运行脚本时出现以下错误:
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
从这里的其他答案来看,大多数人似乎通过使用绝对文件路径而不是相对文件来解决问题,但我看不出这是我的问题。当我 pwd 目录时,我得到了包含在我的代码中的文件路径,所以我看不到哪里出错了。
我的脚本代码如下。
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)
任何帮助都将不胜感激,因为我一直在这个问题上兜圈子,却一无所获。
编辑 - @Osmann Durdag
当我尝试您的第二个解决方案时,我收到以下错误消息:
(base) Edwards-MacBook-Pro:FarneIslands edwibberley$ python Descentlog_manual.py Traceback(最近一次调用最后):文件“Descentlog_manual.py”,第 54 行,在 fitfilelist = FitFileList(directory = '/Users/edwibberley/ Garmin_descent_FIT_process/FarneIslands')文件“Descentlog_manual.py”,第 23 行,在 init 中为 os.listdir(self.directory)中的 fitfilename:FileNotFoundError:[Errno 2] 没有这样的文件或目录:'/Users/edwibberley/Garmin_descent_FIT_process/FarneIslands '(基础)Edwards-MacBook-Pro:FarneIslands edwibberley$ python Descentlog_manual.py /Users/edwibberley/Garmin_descent_FIT_process/FarneIslands Traceback(最近一次调用最后):文件“Descentlog_manual.py”,第 54 行,在 fitfilelist = FitFileList(directory = '/Users/edwibberley/Garmin_descent_FIT_process/FarneIslands') 文件“Descentlog_manual.py”,第 22 行, init print(os.listdir(self.directory)) FileNotFoundError: [Errno 2] 没有这样的文件或目录:'/Users/ edwibberley/Garmin_descent_FIT_process/FarneIslands'(基地)Edwards-Ma cBook-Pro:FarneIslands edwibberley$
【问题讨论】:
-
这听起来可能很傻,但是您确定
/Users/edwibberley/Garmin_descent_FIT_process/FarneIslands存在并且可读吗? -
请尝试
/Users/edwibberley/Garmin_descent_FIT_process/FarneIslands/ -
无关:您可以使用
glob直接获取过滤后的列表,而不必为.fit文件遍历listdir。 stackoverflow.com/questions/2225564/… -
这是我的 .py 脚本保存的目录,我通过将文件夹拖放到终端窗口中来检查它以获得正确的格式。我还尝试了一个不同的目录,因为它最初保存在我的 icloud 中,一位用户建议这可能会导致问题。我还能做些什么来检查它吗?
-
一般没有隐藏的龙,就这样……你的目录不存在。
标签: python