Advertisement
Frixwar

Untitled

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