0

On running this code

def read_text(): quotes = open("C:\Users\rajawatk\Desktop\movie_quotes.txt") contents_of_file = quotes.read() print(contents_of_file) quotes.close() read_text() 

Following was the error message

IOError is displayed

if following code is run by specifying the read permissions

well if we add an r which stands for read permission like

quotes = open(r"C:\Users\rajawatk\Desktop\movie_quotes.txt") 

Voila, The code work i am able to read the movie_quotes.txt file and here is the output

-- Houston, we have a problem. (Apollo 13)

-- Mama always said, life is like a box of chocolates. You never know what you are going to get. (Forrest Gump)

-- You cant handle the truth. (A Few Good Men)

-- I believe everything and I believe nothing. (A Shot in the Dark)

2
  • 1
    "IOError is displayed". Are you saying that the word "IOError" appeared on the screen, or the exact phrase "IOError is displayed" appeared on the screen? Neither of them sound like an ordinary Python stack trace to me. Commented Oct 29, 2015 at 13:33
  • Error: Traceback (most recent call last): File "C:\Python27\movie_quotes.py", line 6, in <module> read_text() File "C:\Python27\movie_quotes.py", line 2, in read_text quotes = open("C:\Users\rajawatk\Desktop\movie_quotes.txt") IOError: [Errno 22] invalid mode ('r') or filename: 'C:\\Users\rajawatk\\Desktop\\movie_quotes.txt' Commented Oct 31, 2015 at 3:30

2 Answers 2

6

well if we add an r which stands for read permission like

That is not what r stands for. An r preceding a string marks it as a raw string, which means that slashes will not be interpreted as escape characters.

"C:\Users\rajawatk" gets intrepreted as "C:\Users" followed by a carriage return followed by "ajawatk".
r"C:\Users\rajawatk" gets intrepreted as "C:\Users" followed by a slash and the letter r, followed by "ajawatk".

You probably don't have a file with a carriage return in the name, so the former version would not be suitable as a parameter to open.

Sign up to request clarification or add additional context in comments.

3 Comments

To prevent any further confusion, one might go for open("C:\\Users\\rajawatk\\Desktop\\movie_quotes.txt") instead. The difference may be more explicitly visible by comparing the result of print("spam\nham\neggs") to print(r"spam\nham\neggs")
Or just use forward slashes
It's fine to use raw strings here - situations like these are exactly why they are in the language. You just... Need to know that they exist and how they work before you can use them.
2

You need to specify the mode in which you want to open the file, and "\" is an escape sequence which will need escaping. In your case.

 quotes = open("C:\\Users\\rajawatk\\Desktop\\movie_quotes.txt", "r") 

Specifying a full path name like you are is, however, poor practice. You should be using os.path to give you a working directory. If you use '/' to specify directories then you don't need to escape, and it works on both windows and linux. Something on the lines of

 my_dir = os.path.dirname(__file__) file_path = os.path.join(my_dir, '../static/', 'movie_quotes.txt') 

Which would look for the the file in a sister static directory to the directory that your application is running from. If you want it in the same directory then.

 my_dir = os.path.dirname(__file__) file_path = os.path.join(my_dir, 'movie_quotes.txt') 

2 Comments

No, "r" is the default mode. You don't need to specify it.
The "r", in this case is needed for the raw string.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.