Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3.2.2 open('C:\file.txt') doesn't work


Admittedly, I am a novice; however, (I think) I did what the tutorial said (for the version I installed) and I can't get Python to read a file. Others have had problems with long file names or paths, but I kept mine short and sweet... so I thought.
The file itself contains one word on a single line.
The print('string') works, len('string') works...

Here's what I got, in IDLE and CMD, even before using the import statements included below:

Python 3.2.2 (default, Sep 4 2011, 09:07:29) [MSC v.1500 64 bit (AMD64)] on win32

Type "copyright", "credits" or "license()" for more information.

import os, csv, urllib

f0txt = ()

f0txt = open("C:\try\in0.txt")

Traceback (most recent call last):

File "<pyshell#4>", line 1, in

f0txt = open("C:\try\in0.txt")

IOError: [Errno 22] Invalid argument: 'C:\try\in0.txt'

like image 569
Matthew Rosso Avatar asked Jan 18 '23 05:01

Matthew Rosso


1 Answers

\ is an escape character. Try open(r"C:\try\in0.txt") or open("C:\\try\\in0.txt").

like image 109
bereal Avatar answered Jan 26 '23 01:01

bereal