Python 3.2.2 open ('C: \ file.txt') not working


Admittedly, I'm a beginner; however (I think), I did what the tutorial said (for the installed version), and I cannot get Python to read the file. Others had problems with long file names or paths, but I kept mine short and clear ... so I thought.
The file itself contains one word on one line.
Print ('string') works, len ('string') works ...

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

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

Enter “copyright,” “credit,” or “license ()” for more information.

import os, csv, urllib

f0txt = ()

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

Traceback (last last call):

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

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

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

+4
source share
1 answer

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

+9
source

Source: https://habr.com/ru/post/1401152/


All Articles