Python Forum

Full Version: [split] AttributeError: 'str' object has no attribute 'read'
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hai everybody,
i write a code to open a txt file by input method but I am getting an error can any body help me.
this is the code

import os

file = input("enter the file name: ")

if os.path.exists(f"{file.lower().strip()}.txt"):
    open(f"{file.lower().strip()}.txt", "r")
    print(file.read())
else:
    print("the file does not exist")
this is the error

Output:
enter the file name: laxmi
Error:
Traceback (most recent call last): File "C:/Users/LAXMIDHAR/PycharmProjects/demo/input fun.py", line 7, in <module> print(file.read()) AttributeError: 'str' object has no attribute 'read' Process finished with exit code 1
This message is for laxmipython:
This is the second time that you have pirated someone else's' thread.
yesterday as well here: https://python-forum.io/Thread-numpy-app...#pid105022
This type of activity can get you banned.
Since you are new, I am not issuing an official warning, next time I or someone else will.
Please be polite, Start your own thread
Thank you
to Larz60+ I didn't copy any ones code I pasted there ( https://python-forum.io/Thread-numpy-app...#pid105022) because I am new to this site so I don't know how to post something in this website. since a long day I am getting this error and I tried my best to resolve this code but I cant so I need some one who help me out on this code.
Okay if it is like this than I am sorry.

it will not happen next time.
thank you so much for your feedback.