-1

My code:

String = open(r"C:\Users\chloe\OneDrive\Documents\Python\Python code\Python text files\Story\VerbJust.txt", "r").read()
print(String)

I have the file stored in the exact folder, but I got an error:``

Traceback (most recent call last):   
  File "C:\Users\chloe\OneDrive\Documents\Python\Python code\StoryClasses.py", line 47, in <module>     
    VerbTo = ReadFile("VerbTo")   
  File "C:\Users\chloe\OneDrive\Documents\Python\Python code\StoryClasses.py", line 41, in ReadFile     
   string = open(w[variable][0], "r").read() 
FileNotFoundError: [Errno 2] No such file or directory: 'C'

Why is this? Can Python not access OneDrive?

Barmar
  • 741,623
  • 53
  • 500
  • 612
Clobro
  • 123
  • 1
  • 8
  • The error message means you're trying to open a file just named `C`, not `C:\pathname` – Barmar Aug 06 '20 at 23:02
  • Oh yeah, sorry! But I'm not sure why it would say that, because I didn't put C. – Clobro Aug 06 '20 at 23:05
  • 2
    You must have done that. The code you posted woin't even run, so it's obviously not what you actually have in the script. Post the real code. – Barmar Aug 06 '20 at 23:08
  • Python doesn't care that it's on OneDrive. The OS handles that. – Barmar Aug 06 '20 at 23:09
  • 1
    Is that really the entire error message? Please copy and paste the full traceback. – Barmar Aug 06 '20 at 23:11
  • My real code is 140 lines long... – Clobro Aug 06 '20 at 23:12
  • `Traceback (most recent call last): File "C:\Users\chloe\OneDrive\Documents\Python\Python code\StoryClasses.py", line 47, in VerbTo = ReadFile("VerbTo") File "C:\Users\chloe\OneDrive\Documents\Python\Python code\StoryClasses.py", line 41, in ReadFile string = open(w[variable][0], "r").read() FileNotFoundError: [Errno 2] No such file or directory: 'C'` – Clobro Aug 06 '20 at 23:12
  • 1
    If `w[variable]` contains the filename, `w[variable][0]` contains the first character of the filename. – Barmar Aug 06 '20 at 23:13
  • Oh, thanks. That was a stupid mistake... Sorry. – Clobro Aug 06 '20 at 23:15
  • Now you understand why I asked you to paste the actual code? – Barmar Aug 06 '20 at 23:16

2 Answers2

1

In this line:

string = open(w[variable][0], "r").read()

it appears that w[variable] contains the filename. Adding [0] to that uses just the first character of the filename. Get rid of that.

string = open(w[variable], "r").read()
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

This error occurs because the quotation marks are formatted incorrectly.

Also, I suspect the variable name you chose, "String", may cause some issues.

Try:

string = open(r"filepath", "r").read()
print(string)
davetherock
  • 224
  • 1
  • 2
  • 12