0
Q:

python write

file = open(“testfile.txt”,”w”) 
 
file.write(“Hello World”) 
file.write(“This is our new text file”) 
file.write(“and this is another line.”) 
file.write(“Why? Because we can.”) 
 
file.close() 
36
with open("file.txt", "w") as file:
  for line in ["hello", "world"]:
    file.write(line)
1
with open(filename,"w") as f:
  f.write('Hello World')
15
#there are many modes you can open files in. r means read.
file = open('C:\Users\yourname\files\file.txt','r')
text = file.read()

#you can write a string to it, too!
file = open('C:\Users\yourname\files\file.txt','w')
file.write('This is a typical string')

#don't forget to close it afterwards!
file.close()
6
fin = open("NAME.txt", 'r')
body = fin.read().split("\n")
line = fin.readline().strip()
6

New to Communities?

Join the community