Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 5.4 File Input Output
- #Part 1
- sports = ["baseball", "basketball", "volleyball"] #Makes a string that has 3 attributes.
- inFile = open("sports.txt", "a") #The File is created in the local drive that the program is running in. This happens because
- #the user did not tell the computer where to save the file so by default it does it in the same directory.
- for x in sports:
- inFile.write(str((x + "\n"))) #Prints every line in the list on a seperate line in the text file.
- inFile.close() #Files need to be closed when they are opened just like anything else.
- print("Done! Data is saved in file: sports.txt") #Prints Text.
- #Part 2
- sportsList = open ('sports.txt') #Opens the txt file to be read or written to.
- sp = sportsList.read() #Reads the contents of the text file.
- print (sp) #Prints the contents of the text file.
- sportsList.close() #Closes the file just like in part one.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement