Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

'tuple' object has no attribute 'read'

I am trying to create a program that will read from a file on my computer.
The file consists of simply door numbers from 1-150 within a bracket.

The error returns at line 6. print(file.read())

your text#opening_a_file.py

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

your textfile = "This PC/C:/Python Programming/Doors.txt","r"

your textprint("read function: ")
your textprint(file.read())
your textprint()

your textfile.seek(0)

I tried renaming the file absolute path.
I edited line 3 as well to read as:
file = open("This PC/c:/Python Programming/Doors.txt","r")
but that did not work.

Help would be much appreciated. Thank you

>Solution :

This is because the variable file is a tuple. That is what happens when you define it in tuple format: var = value1, value2

So, in your case, you want a file object, so you need to do this:

file = open("C:/Python Programming/Doors.txt","r")

For the sake of the answer, here is the full code sum-up:

file = open("C:/Python Programming/Doors.txt","r")

print("read function: ")
print(file.read())
print()

file.seek(0)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading