Python Opening Files in Python - Newbie Question

  • Thread starter Thread starter zeion
  • Start date Start date
  • Tags Tags
    Python
AI Thread Summary
The discussion centers around the correct syntax for opening a file in Python, specifically a text file located on the desktop. The user is attempting to open "testfile.txt" using two different methods, but neither is successful. Key points include the importance of specifying the correct file path and recognizing that backslashes in Windows file paths can lead to errors due to escape characters. It is advised to use double backslashes or single forward slashes to avoid issues with special characters. Additionally, it is noted that the file must be accessible from the environment where the Python code is executed, such as IDLE or a script.
zeion
Messages
455
Reaction score
1

Homework Statement



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.

Homework Equations





The Attempt at a Solution

 
Technology news on Phys.org
Code:
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?
 
Ok I realized I couldn't open the file in IDLE.. but only with the actual program.
 
Either use double backslashes, '\\', or single forward slashes, '/'. Otherwise you have characters like '\t', which is the tab character, embedded in your filename.
 
Wow...it's been so long since I've used Python on Windows that I forgot about that.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
2
Views
782
Replies
4
Views
3K
Replies
8
Views
1K
Replies
16
Views
4K
Replies
10
Views
2K
Replies
3
Views
3K
Replies
11
Views
2K
Back
Top