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

How to take a input in python and change the format into binary?

This is the whole code

#input from the user
Text = input("Enter a String :) : ")

#this portion will create a file
with open(Text, 'w') as f:
    file = f.write("this is a test file")


#loops through

for i in range(1,100):
#this is creating a new file
    a = open(Text, "a")
    b = a.write("\n this will append to the file")
    a.close() 
    # print(i)

#this portion is reading from the file
f = open(Text, "r")
d = f.read()
print(d,end="")
f.close()

I'm trying to take the input in string, but i want it to save the file in text format
I’m a beginner, just trying things.

what i want is, that it creates a file in .txt

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

like,

input: helloWorld

output: helloWorld.txt

>Solution :

To create a file in the .txt format, you can simply add the ‘.txt’ extension to the file name when you create it. To do this, you can modify the line where you create the file like this:

with open(Text + '.txt', 'w') as f:
    file = f.write("this is a test file")

This will create a file with the name Text + ‘.txt’, for example, if Text is ‘helloWorld’, the file will be named ‘helloWorld.txt’.

You will also need to make a similar change to the lines where you open the file in append mode (‘a’) to make sure you are appending to the correct file.

a = open(Text + '.txt', "a")

Finally, when you open the file to read from it, you will also need to make sure you are opening the correct file with the correct extension.

f = open(Text + '.txt', "r")

I hope this helps!

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