Register to reply

Newbie python question

by zeion
Tags: newbie, python
Share this thread:
Jun10-11, 08:51 PM
P: 467
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')


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

but neither works.

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


2. Relevant equations

3. The attempt at a solution
Phys.Org News Partner Science news on
Scientists develop 'electronic nose' for rapid detection of C. diff infection
Why plants in the office make us more productive
Tesla Motors dealing as states play factory poker
Jun10-11, 10:42 PM
PF Gold
jhae2.718's Avatar
P: 1,160
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?
Jun15-11, 11:01 PM
P: 467
Ok I realized I couldn't open the file in IDLE.. but only with the actual program.

Jun16-11, 12:00 AM
P: 900
Newbie python question

Either use double backslashes, '\\', or single forward slashes, '/'. Otherwise you have characters like '\t', which is the tab character, embedded in your filename.
Jun17-11, 12:26 AM
PF Gold
jhae2.718's Avatar
P: 1,160's been so long since I've used Python on Windows that I forgot about that.

Register to reply

Related Discussions
Compile Python, Matlab and Python Programming & Computer Science 3
Python question Engineering, Comp Sci, & Technology Homework 1
Beginner Python question Engineering, Comp Sci, & Technology Homework 10
Fortran/Python newbie tired of slogging through anti-tutorials online. Programming & Computer Science 7
Python question Engineering, Comp Sci, & Technology Homework 2