0

Although my folder is in the correct path, the function can't find it.

I am relatively new to programming, maybe some obvious mistakes?

import backtrader as bt

cerebro = bt.Cerebro()

data = bt.feeds.GenericCSVData(dataname=r'D:\Programme\Visual Studio Code\Projekte\D_Candles.csv', dtformat=2)

cerebro.adddata(data)

cerebro.run()

cerebro.plot()

All dependencies and packages were installed.

Error Message:

PS D:\Programme\Visual Studio Code\Projekte> & 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:\Programme\Visual Studio Code\Projekte\Trading\backtest.py", line 9, in <module>
    cerebro.run()
  File "C:\Users\William\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\backtrader\cerebro.py", line 1127, in run
    runstrat = self.runstrategies(iterstrat)
  File "C:\Users\William\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\backtrader\cerebro.py", line 1210, in runstrategies
    data._start()
  File "C:\Users\William\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\backtrader\feed.py", line 203, in _start
    self.start()
  File "C:\Users\William\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\backtrader\feeds\csvgeneric.py", line 88, in start 
    super(GenericCSVData, self).start()
  File "C:\Users\William\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\backtrader\feed.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:\Programme\Visual Studio Code\Projekte>

What I tried so far:

  • just tiping the folder name, not the whole path
  • switching the backslashes, tried //, \, /, \ and with & without r
  • copied the folder in an other path
  • it is not because of spaces between words

I really appreciate your help. Even if you just know an alternative function I could try...

  • "...and many more here..." - that may not contain the clue for what's wrong to you, but it may to us. Always include the full stacktrace. StackOverflow will give the box a scrollbar if it's too long, so don't worry about how it looks. – Grismar Jan 21 '23 at 23:20
  • `FileNotFoundError: [Errno 2] No such file or directory:` Does the error actually print the name of the missing file? You left that part out... – John Gordon Jan 21 '23 at 23:37
  • Your program script is in the `Projekte\Trading` directory. Do you expect the csv file to be in that same directory? – John Gordon Jan 21 '23 at 23:39
  • I edited the Error Message, so that you fully can see what's going on. Yes, it prints the whole file path. And yes, I expect that the CSV file is in that same directory...because it is. – theJavaNoob Jan 23 '23 at 16:35
  • Are you sure that `D_Candles.csv` is the name of your file? I can see in your output that `D:\Programme\Visual Studio Code\Projekte` should exist, try running `dir` there. – Abdul Aziz Barkat Jan 23 '23 at 16:49

1 Answers1

1

try this

r'D:\Programme\Visual Studio Code\Projekte\D_Candles.csv'

it indicates the variable as a raw string which treats the backslash character as a literal character. it is useful when a string needs to contain a backslash, such as for a regular expression or Windows directory path

Scalobe
  • 23
  • 5
  • Your advice is not bad advice, but not the issue for OP. They indicate they already tried switching the backslashes for slashes and none of `\P`, `\D` and `\V` are special characters. I.e. `'\\P' == '\P'` and `r'\P' == '\P'`. – Grismar Jan 21 '23 at 23:23
  • I tried that and put it in as you see now in my edited version. But that did not solve the problem – theJavaNoob Jan 23 '23 at 16:39