I am making a program in python.
When we select some text and press a key it will save it into a txt
file. Does someone know how to save selected text into a file in windows?
CodePudding user response:
input="Your selected sentence"
with open('file.txt', 'w') as f:
f.write(input)
If file.txt
doesn't exist, the open
method will create it.
CodePudding user response:
Do you know file handling
? If yes, here's your answer. If no, learning it would help you solve this question. It is a concept which deals with reading and writing in a file using python. Acc to your question, we'll open a file, take an input, write that input in a file and then close the file. You should open the file in append mode (I prefer) because everytime you write in the file, nothing will effect the old data. If you open it in write mode, everytime it'll overwrite the file resulting in writing only the last data and losing the previous ones. To open the file in append mode, type "a" in the open statement. Let's say your file name is writing.txt then your code:
f=open("writing.txt","a")
f_input=input("Enter your text= ")
f.write(f_input)
f.close()
Now when you check your file, you'll see your text written