Instead of using write(), what are the other way to write to a text file in Python 2 and 3?
file = open('filename.txt', 'w')
file.write('some text')
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use the
print_functionfuture import to get theprint()behaviour from python3 in python2:If you do not want that function to append a linebreak at the end, add the
end=''keyword argument to theprint()call.However, consider using
f.write('some text')as this is much clearer and does not require a__future__import.