1file = open("text.txt", "w")
2file.write("Your text goes here")
3file.close()
4'r' open for reading (default)
5'w' open for writing, truncating the file first
6'x' open for exclusive creation, failing if the file already exists
7'a' open for writing, appending to the end of the file if it exists
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)