-1

I am trying to read a text file on my hard drive via python with the following script:

fileref = open("H:\CloudandBigData\finalproj\BeautifulSoup\twitter.txt","r")

But it is giving the following error:

IOError                                   Traceback (most recent call last)
<ipython-input-2-4f422ec273ce> in <module>()
----> 1 fileref = open("H:\CloudandBigData\finalproj\BeautifulSoup\twitter.txt","r")

IOError: [Errno 2] No such file or directory: 'H:\\CloudandBigData\x0cinalproj\\BeautifulSoup\twitter.txt'

I also tried with other way:

with open('H:\CloudandBigData\finalproj\BeautifulSoup\twitter.txt', 'r') as f:
    print f.read()

Ended up with the same error. The text file is present in the directory specified.

Shantanu Mitra
  • 31
  • 1
  • 1
  • 6
  • 3
    Escape your backslashes properly –  Oct 09 '13 at 03:49
  • Use a raw string (`r"..."`) instead. The backslashes are causing you problems. For example, `"\f" == "\x0c"`. Even better, use forward slashes - Windows doesn't care. – Tim Peters Oct 09 '13 at 03:50
  • Sorry I didn't get you.How to Escape properly? Do you want me to write the file path manually, because I just copy pasted the path. – Shantanu Mitra Oct 09 '13 at 03:52
  • 1
    Change each backslash to two backslashes, **or** use a raw string, **or** use forward slashes instead. – Tim Peters Oct 09 '13 at 03:55
  • @TimPeters **or** use `os.path.join` as in my answer... that really removes the possibility of problems like this. – SethMMorton Oct 09 '13 at 03:59

2 Answers2

6

Replace

fileref = open("H:\CloudandBigData\finalproj\BeautifulSoup\twitter.txt","r")

with

fileref = open(r"H:\CloudandBigData\finalproj\BeautifulSoup\twitter.txt","r")

Here, I have created a raw string (r""). This will cause things like "\t" to not be interpreted as a tab character.

Another way to do it without a raw string is

fileref = open("H:\\CloudandBigData\\finalproj\\BeautifulSoup\\twitter.txt","r")

This escapes the backslashes (i.e. "\\" => \).


An even better solution is to use the os module:

import os
filepath = os.path.join('H:', 'CloudandBigData', 'finalproj', 'BeautifulSoup', 'twitter.txt')
fileref = open(filepath, 'r')

This creates your path in an os-independent way so you don't have to worry about those things.


One last note... in general, I think you should use the with construct you mentioned in your question... I didn't in the answer for brevity.

SethMMorton
  • 45,752
  • 12
  • 65
  • 86
  • I am still getting the same error: --------------------------------------------------------------------------- IOError Traceback (most recent call last) in () ----> 1 fileref = open("H:\\CloudandBigData\\finalproj\\BeautifulSoup\\twitter.txt","r") IOError: [Errno 2] No such file or directory: 'H:\\CloudandBigData\\finalproj\\BeautifulSoup\\twitter.txt' – Shantanu Mitra Oct 09 '13 at 04:01
  • --------------------------------------------------------------------------- IOError Traceback (most recent call last) in () ----> 1 fileref = open(r"H:\CloudandBigData\finalproj\BeautifulSoup\twitter.txt","r") IOError: [Errno 2] No such file or directory: 'H:\\CloudandBigData\\finalproj\\BeautifulSoup\\twitter.txt' – Shantanu Mitra Oct 09 '13 at 04:03
  • Do we need to install any libraries in python prior to reading a text file? – Shantanu Mitra Oct 09 '13 at 04:04
  • If you you are still getting the "No such file or directory", are you sure that the file exists? And no, you don't need any libraries. – SethMMorton Oct 09 '13 at 04:05
  • I would like to point out the error isn't *exactly* the same. In your question, the name of the file was messed up because of the special characters (specifically `\f` and `\t`.) In the traceback you posted in the above comment, the filename is "correct", so if you are still getting the error it must be because the file does not exist. – SethMMorton Oct 09 '13 at 04:10
  • I just realised the mistake. I am running python in Wakari which is running cloud. – Shantanu Mitra Oct 09 '13 at 05:02
1

I was encountering same problem. This problem resulted due to different file path notation Python.
For example, filepath in Windows reads with backward slash like: "D:\Python\Project\file.txt"

But Python reads file path with forward slash like: "D:/Python/Project/file.txt"

I used r"filepath.txt" and "os.path.join" and "os.path.abspath" to no relief. os library also generates file path in Windows notation. Then I just resorted to IDE notation.

You don't encounter this error if "file.txt" is located in same directory, as filename is appended to working directory.

PS: I am using Python 3.6 with Spyder IDE on Windows machine.

Raj
  • 23
  • 6