Answers for "write text in 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
19

python write to file

with open(filename,"w") as f:
  f.write('Hello World')
Posted by: Guest on March-30-2020
1

write txt python

with open('readme.txt', 'w') as f:
    f.write('readme')
Code language: JavaScript (javascript)
Posted by: Guest on October-23-2021

Python Answers by Framework

Browse Popular Code Answers by Language