1file = open(“testfile.txt”,”w”)
2
3file.write(“Hello World”)
4file.write(“This is our new text file”)
5file.write(“and this is another line.”)
6file.write(“Why? Because we can.”)
7
8file.close()
1#the way i learned it
2#plus explanations
3
4from sys import * #this is for making it more flexible
5
6#so now we gotta open a file to write in
7
8f = open("haha.txt", "w") # the w means WRITING
9#now we gotta write something
10
11f.write(str(argv[1])) # the str means change it into a string of letters
12#argv[1] means it's the thing you type after the python run thing
13#for example: "python run.py helo"
14
15#now we gotta finish it
16f.close()
17
18#if you want to see whats in it...
19
20f = open("haha.txt", "r") #r means READING
21print(f.read()) # this prints what you wrote
22
23#example input and output
24
25# python run.py helo
26# helo
1with open('readme.txt', 'w') as f:
2 f.write('readme')
3Code language: JavaScript (javascript)