Code Point
September 08, 2010, 08:30:05 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Geshi Sintax Hilighting Mod Installed. Tens of languages available.
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Can't open file  (Read 2610 times)
Telplate
Jr. Member
*

Karma: 1
Offline Offline

Posts: 4


« on: September 10, 2006, 10:00:19 PM »

I'm trying to open a file in Windows with this code:

Code
f = open('C:\my_programs\records.txt')
print f.read()

And it is returning this error:

Code
C:\my_programs>openfile.py
Traceback (most recent call last):
 File "C:\my_programs\openfile.py", line 1, in ?
   f = open('C:\my_programs\records.txt')
IOError: [Errno 2] No such file or directory: 'C:\\my_programs\records.txt'
Logged
Alter Lobo
Global Moderator
Jr. Member
*****

Karma: 10
Offline Offline

Posts: 65


« Reply #1 on: September 10, 2006, 10:45:00 PM »

The backslashes in 'C:\my_programs\records.txt' are escape characters an turns the \r  into a carriage return character.

To avoid the interpretation of backslashes in strings place the r letter before the string:

Code
f = open(r'C:\my_programs\records.txt')
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!