虽然我的文件夹在正确的路径上,但是函数找不到它。
我对编程比较陌生,也许有一些明显的错误?
import backtrader as bt
cerebro = bt.Cerebro()
data = bt.feeds.GenericCSVData(dataname=r'D:ProgrammeVisual Studio CodeProjekteD_Candles.csv', dtformat=2)
cerebro.adddata(data)
cerebro.run()
cerebro.plot()
所有依赖项和包已安装。
错误信息:
PS D:ProgrammeVisual Studio CodeProjekte> & C:/Users/William/AppData/Local/Microsoft/WindowsApps/python3.10.exe "d:/Programme/Visual Studio Code/Projekte/Trading/backtest.py"
Traceback (most recent call last):
File "d:ProgrammeVisual Studio CodeProjekteTradingbacktest.py", line 9, in <module>
cerebro.run()
File "C:UsersWilliamAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packagesbacktradercerebro.py", line 1127, in run
runstrat = self.runstrategies(iterstrat)
File "C:UsersWilliamAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packagesbacktradercerebro.py", line 1210, in runstrategies
data._start()
File "C:UsersWilliamAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packagesbacktraderfeed.py", line 203, in _start
self.start()
File "C:UsersWilliamAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packagesbacktraderfeedscsvgeneric.py", line 88, in start
super(GenericCSVData, self).start()
File "C:UsersWilliamAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packagesbacktraderfeed.py", line 674, in start
self.f = io.open(self.p.dataname, 'r')
FileNotFoundError: [Errno 2] No such file or directory: 'D:\Programme\Visual Studio Code\Projekte\D_Candles.csv'
PS D:ProgrammeVisual Studio CodeProjekte>
我试过了:
- 只是提示文件夹名称,而不是整个路径
- 切换反斜杠,尝试//,,/,和&没有r
- 将文件夹复制到其他路径
- 不是因为单词之间有空格
我真的很感谢你的帮助。即使你只知道一个替代函数,我也可以尝试…
try this
r'D:ProgrammeVisual Studio CodeProjekteD_Candles.csv'
表示变量为原始字符串,将反斜杠字符视为文字字符。当字符串需要包含反斜杠时很有用,例如用于正则表达式或Windows目录路径