Answers for "read and write strings in txt file with python"

2

how to write to a text file in python

#the way i learned it
#plus explanations

from sys import * #this is for making it more flexible

#so now we gotta open a file to write in

f = open("haha.txt", "w") # the w means WRITING
#now we gotta write something

f.write(str(argv[1])) # the str means change it into a string of letters
#argv[1] means it's the thing you type after the python run thing
#for example: "python run.py helo"

#now we gotta finish it
f.close()

#if you want to see whats in it...

f = open("haha.txt", "r") #r means READING
print(f.read()) # this prints what you wrote

#example input and output

#            python run.py helo
#   helo
Posted by: Guest on May-17-2021
1

print in text file python

price = 33.3
with open("Output.txt", "w") as text_file:
    text_file.write("Purchase Amount: %s price %f" % (TotalAmount, price))
Posted by: Guest on May-15-2020

Code answers related to "read and write strings in txt file with python"

Python Answers by Framework

Browse Popular Code Answers by Language