1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Newbie python question

  1. Jun 10, 2011 #1
    1. The problem statement, all variables and given/known data

    Hi, this might be silly but I'm not sure what the syntax is for the read function...

    I'm trying to open a file on my desktop, it is a text file called testfile.txt, how do I make python know where to find it?

    I go:

    f = open('C:\Users\Truman\Desktop\testfile.txt', 'r')

    or

    f = open('testfile.txt', 'r')

    but neither works.

    Does the file need to be in the same directory as where I installed Python?

    Thanks.

    2. Relevant equations



    3. The attempt at a solution
     
  2. jcsd
  3. Jun 10, 2011 #2

    jhae2.718

    User Avatar
    Gold Member

    Code (Text):

    f = open(filename, mode)
     
    should work if you specify the file path.

    Are you getting an IOError? Also, if you directly use the file() constructor, can you open the file?
     
  4. Jun 15, 2011 #3
    Ok I realized I couldn't open the file in IDLE.. but only with the actual program.
     
  5. Jun 16, 2011 #4
    Either use double backslashes, '\\', or single forward slashes, '/'. Otherwise you have characters like '\t', which is the tab character, embedded in your filename.
     
  6. Jun 17, 2011 #5

    jhae2.718

    User Avatar
    Gold Member

    Wow...it's been so long since I've used Python on Windows that I forgot about that.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Newbie python question
  1. Python question (Replies: 2)

  2. Python question (Replies: 1)

  3. Python 3 Question (Replies: 3)

Loading...