Python 使用Astropy打开FITS文件会导致FileNotFoundError

Python 使用Astropy打开FITS文件会导致FileNotFoundError,python,astropy,fits,Python,Astropy,Fits,当我使用 fits_datasweep_gal = fits.open('Macintosh HD/Users/lingxuan/Downloads/datasweep-index-gal.fits') 要在Jupyter笔记本上打开FITS文件,它将返回: FileNotFoundError Traceback (most recent call last) <ipython-input-13-e5886f60eba2> in &l

当我使用

fits_datasweep_gal = fits.open('Macintosh HD/Users/lingxuan/Downloads/datasweep-index-gal.fits')
要在Jupyter笔记本上打开FITS文件,它将返回:

FileNotFoundError                         Traceback (most recent call last)
<ipython-input-13-e5886f60eba2> in <module>
----> 1 fits_datasweep_gal = fits.open('Macintosh HD/Users/lingxuan/Downloads/datasweep-index-gal.fits')

~/anaconda3/lib/python3.7/site-packages/astropy/io/fits/hdu/hdulist.py in fitsopen(name, mode, memmap, save_backup, cache, lazy_load_hdus, **kwargs)
    149 
    150     return HDUList.fromfile(name, mode, memmap, save_backup, cache,
--> 151                             lazy_load_hdus, **kwargs)
    152 
    153 

~/anaconda3/lib/python3.7/site-packages/astropy/io/fits/hdu/hdulist.py in fromfile(cls, fileobj, mode, memmap, save_backup, cache, lazy_load_hdus, **kwargs)
    388         return cls._readfrom(fileobj=fileobj, mode=mode, memmap=memmap,
    389                              save_backup=save_backup, cache=cache,
--> 390                              lazy_load_hdus=lazy_load_hdus, **kwargs)
    391 
    392     @classmethod

~/anaconda3/lib/python3.7/site-packages/astropy/io/fits/hdu/hdulist.py in _readfrom(cls, fileobj, data, mode, memmap, save_backup, cache, lazy_load_hdus, **kwargs)
   1037             if not isinstance(fileobj, _File):
   1038                 # instantiate a FITS file object (ffo)
-> 1039                 fileobj = _File(fileobj, mode=mode, memmap=memmap, cache=cache)
   1040             # The Astropy mode is determined by the _File initializer if the
   1041             # supplied mode was None

~/anaconda3/lib/python3.7/site-packages/astropy/utils/decorators.py in wrapper(*args, **kwargs)
    501                         # one with the name of the new argument to the function
    502                         kwargs[new_name[i]] = value
--> 503             return function(*args, **kwargs)
    504 
    505         return wrapper

~/anaconda3/lib/python3.7/site-packages/astropy/io/fits/file.py in __init__(self, fileobj, mode, memmap, overwrite, cache)
    176             self._open_fileobj(fileobj, mode, overwrite)
    177         elif isinstance(fileobj, str):
--> 178             self._open_filename(fileobj, mode, overwrite)
    179         else:
    180             self._open_filelike(fileobj, mode, overwrite)

~/anaconda3/lib/python3.7/site-packages/astropy/io/fits/file.py in _open_filename(self, filename, mode, overwrite)
    553 
    554         if not self._try_read_compressed(self.name, magic, mode, ext=ext):
--> 555             self._file = fileobj_open(self.name, IO_FITS_MODES[mode])
    556             self.close_on_error = True
    557 

~/anaconda3/lib/python3.7/site-packages/astropy/io/fits/util.py in fileobj_open(filename, mode)
    386     """
    387 
--> 388     return open(filename, mode, buffering=0)
    389 
    390 

FileNotFoundError: [Errno 2] No such file or directory: 'Macintosh HD/Users/lingxuan/Downloads/datasweep-index-gal.fits'
FileNotFoundError回溯(最近一次调用)
在里面
---->1 fits\u datasweep\u gal=fits.open('Macintosh HD/Users/lingxuan/Downloads/datasweep-index-gal.fits'))
fitsopen中的~/anaconda3/lib/python3.7/site-packages/astropy/io/fits/hdu/hdulist.py(名称、模式、memmap、保存、备份、缓存、延迟加载、hdus、**kwargs)
149
150返回HDUList.fromfile(名称、模式、memmap、save\u备份、缓存、,
-->151延迟加载HDU,**kwargs)
152
153
fromfile中的~/anaconda3/lib/python3.7/site-packages/astropy/io/fits/hdu/hdulist.py(cls、fileobj、mode、memmap、save\u backup、cache、lazy\u load\u hdus、**kwargs)
388返回cls.\u readfrom(fileobj=fileobj,mode=mode,memmap=memmap,
389保存备份=保存备份,缓存=缓存,
-->390惰性负载=惰性负载,**kwargs)
391
392@classmethod
~/anaconda3/lib/python3.7/site-packages/astropy/io/fits/hdu/hdulist.py in\u readfrom(cls、fileobj、数据、模式、memmap、保存备份、缓存、延迟加载\u hdus、**kwargs)
1037如果不存在(fileobj,_文件):
1038#实例化FITS文件对象(ffo)
->1039 fileobj=_文件(fileobj,mode=mode,memmap=memmap,cache=cache)
1040#如果
1041#提供的模式为无
包装中的~/anaconda3/lib/python3.7/site-packages/astropy/utils/decorators.py(*args,**kwargs)
501#一个函数的新参数名
502 kwargs[新名称[i]]=值
-->503返回函数(*args,**kwargs)
504
505返回包装器
~/anaconda3/lib/python3.7/site-packages/astropy/io/fits/file.py in\uuuuuuu init\uuuuuuu(self、fileobj、mode、memmap、overwrite、cache)
176自动打开文件对象(文件对象,模式,覆盖)
177 elif isinstance(文件对象,str):
-->178 self.\u open\u文件名(fileobj,mode,overwrite)
179其他:
180 self.\u open\u filelike(fileobj,mode,overwrite)
~/anaconda3/lib/python3.7/site-packages/astropy/io/fits/file.py in\u open\u filename(self、filename、mode、overwrite)
553
554如果不是self.\u try\u read\u compressed(self.name,magic,mode,ext=ext):
-->555 self.\u file=fileobj\u open(self.name,IO\u FITS\u MODES[模式])
556 self.close\u on\u错误=真
557
fileobj_open中的~/anaconda3/lib/python3.7/site-packages/astropy/io/fits/util.py(文件名,模式)
386     """
387
-->388返回打开(文件名、模式、缓冲=0)
389
390
FileNotFoundError:[Errno 2]没有这样的文件或目录:“Macintosh HD/Users/lingxuan/Downloads/datasweep-index-gal.fits”

我该怎么办?

从路径中删除
Macintosh HD
部件

fits_datasweep_gal = fits.open('/Users/lingxuan/Downloads/datasweep-index-gal.fits')

从路径中删除Macintosh HD的
part

fits_datasweep_gal = fits.open('/Users/lingxuan/Downloads/datasweep-index-gal.fits')

作为对“我应该怎么做?”的一般回答,这样您就可以更轻松地处理这样的问题:虽然
astropy.io.fits
代码很复杂,但在回溯的最后,您可以看到引发
FileNotFoundError
的代码是
打开的(文件名、模式、缓冲=0)
。暂时忘记
模式
缓冲
部分:最重要的是它调用了
open(filename)
。默认情况下,是用于打开文件进行读取的Python内置程序。您可以尝试调用
open()
直接在您给出的路径上,您将得到相同的错误。或者甚至
导入操作系统
,然后检查
os.path.exists(filename)
以查看它是否是您系统上的现有路径。作为“我应该怎么做?”?“这样您就可以更轻松地处理这样的问题:虽然
astropy.io.fits
代码很复杂,但在回溯的最后,您可以看到引发
FileNotFoundError
的代码是
打开的(文件名、模式、缓冲=0)
。暂时忘记
模式
缓冲
部分:最重要的是它正在调用
open(filename)
。是用于打开文件的Python内置程序,默认情况下用于读取。您可以尝试直接在给定的路径上调用
open()
,您将得到相同的错误。甚至
导入os
,然后检查
os.path.exists(文件名)
以查看它是否是系统上的现有路径。