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

specific line in file to string

with open("Status.txt", "x") as Status: #makes a file 
    UserName = Status.read() #reads the file

with open("Status.txt", "w") as StatusFileWrite:
    StatusFileWrite.write("Hello " + UserName + " :)")

is the code I wrote and it says a error that it can’t read the contents if the file
and how do I change instantly change that the file has when it is created

how do I specify what line it reads

I was ecspecting it to make the string to the contents of the file

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

this Is the error

UserName = Status.read()
           ^^^^^^^^^^^^^

io.UnsupportedOperation: not readable

>Solution :

First, write to the file

with open("Status.txt", "w") as StatusFileWrite:
    StatusFileWrite.write("Hello User :)")

Now, read from the file

with open("Status.txt", "r") as Status:
    UserName = Status.readline()  # Reads the first line
print("Read from file:", UserName)
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