2

I'm trying to read in a text file to work with Word Clouds. Here is the syntax I'm trying:

# Read the whole text.
text = open(r'C:\Users\mswitajski\Desktop\alice.txt').read()

But I keep getting the following error:

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\mswitajski\\Desktop\\alice.txt'

I've triple checked the file name, tried reading it as a raw file, changed the slashes and everything but I continue to get the same error.

halfer
  • 19,824
  • 17
  • 99
  • 186
Mark Switajski
  • 59
  • 1
  • 1
  • 6

4 Answers4

1

Well, if someone reaches up to here and still could not find the solution then here is the more pythonic way of doing the absolute path in windows.

Instead of using:

text = open(r'C:\Users\mswitajski\Desktop\alice.txt').read()

use os.sep, in conjunction of os.path.join like the following:

import os    
text = open(os.path.join('C:', os.sep, 'Users', 'mswitajski', 'Desktop', 'alice.txt')).read()
rs_punia
  • 421
  • 2
  • 6
  • 17
0

Try changin the path to this"

'C:\Users\mswitajski\Desktop/alice.txt'

Sometimes windows won't find/recognize the file path when the file is specified like this

'C:\Users\mswitajski\Desktop\alice.txt'

In the answer it shows up as only one \ but you still need 2 like your previous path. The only difference is the last slash /. Hope that works.

0

At your text raw file (alice.txt) try delete the .txt. The file probably is named alice.txt.txt I face the same issue and solve it by deleted the .txt.

0

I had to use double slashes instead of one, because python interpreted it as a escape sequence. My final string was:

C:\\Users\\ArpitChinmay's\\AppData\\Roaming\\Code\\User\\globalStorage\\moocfi.test-my- 
code\\tmcdata\\TMC workspace\\Exercises\\hy\\hy-data-analysis-with-python- 
2020\\part02-e04_word_frequencies\\src\\alice.txt

However, it worked this way too,

C:\\Users\\Arpit Chinmay's\\AppData\\Roaming\\Code\\User\\globalStorage\\moocfi.test- 
my-code\\tmcdata\\TMC workspace\\Exercises\\hy\\hy-data-analysis-with-python- 
2020\\part02-e04_word_frequencies\\src/alice.txt
Arpit Chinmay
  • 313
  • 5
  • 16
  • But make sure you copy the correct source file path before that. Sometimes the filename may contain the extension e.g. "alice.txt" so it would be saved in the system as alice.txt.txt – Arpit Chinmay Dec 26 '20 at 16:34