Thread: file error

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Canada
    Posts
    185
    Rep Power
    0

    file error


    Can anyone tell me why i keep getting the following syntax errors:
    Code:
    f = open( r "C:\Python23\test.txt")
    SyntaxError: invalid syntax
    >>> f = open( r 'C:Python23\test.txt')
    SyntaxError: invalid syntax
    i've saved the text file in python's path and if i don't give open a path, just the name of the file, it opens fine. What is the invalid syntax here?
    "In theory, there is no difference between theory and practice.
    But, in practice, there is."

  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    14
    those spaces are invalid syntax, the "r" has to be the character immediately preceding the quotes if you want to use raw strings

    also, use file and not open, open is deprecated
    Debian - because life's too short for worrying.
    Best. (Python.) IRC bot. ever.
  4. #3
  5. No Profile Picture
    Python/RDF Freak
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    14
    Rep Power
    0
    Hi maybe this works:

    Code:
    f = open("C:\Python23\test.txt", "r")
  6. #4
  7. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    A combination of the two replies:
    Code:
    f = file(r"C:\Python23\test.txt", "r")
    Read access is the default mode so you could leave the comma "r" bit out, but I wouldn't.

    Grim
    Last edited by Grim Archon; March 19th, 2004 at 08:33 AM.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Canada
    Posts
    185
    Rep Power
    0
    Thanks guys. Appreaciate your help.
    "In theory, there is no difference between theory and practice.
    But, in practice, there is."


IMN logo majestic logo threadwatch logo seochat tools logo