I m trying to open a file in read mode , my program is below ...

print "------"
file = open("C:\Lakhan\Pytonpro\file.txt", "r")
print file

but it shows following error

Traceback (innermost last):
File "C:\Program Files\Python\Pythonwin\pywin\framework\scriptutils.py", line 165, in RunScript
exec f in __main__.__dict__
File "C:\Documents and Settings\Administrator\Desktop\Python\A001.py", line 11, in ?
file = open("C:\Lakhan\Pytonpro\file.txt", "r")
IOError: (22, 'Invalid argument')

can any one help me.

Thanks in advance

Recommended Answers

All 4 Replies

Use forward slashes instead of backward in your file-name. (so c:/program files/python ... etc)
A backslash is used as an escape-character to print out tabs,newlines, etc. Read this for more info


thank u for your reply..
after it going to show error

<open file 'C:/Lakhan/Pytonpro/file.txt', mode 'r' at b4d488>

and my new code . .

file1 = open ('C:/Lakhan/Pytonpro/file.txt', 'r')
print file1

after it going to show error

<open file 'C:/Lakhan/Pytonpro/file.txt', mode 'r' at b4d488>

That's not an error. You told the interpreter to print file1 and it did :) The interpreter is telling you that file1 = an open file (filename), mode 'r' at (memoryadress)

But I can imagine that this was not what you wanted to see. You probably wanted to see the contents of the file. So you'll have to mention that to the interpreter: print file1.readlines()

thank u i got it . .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.