Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. import os
  2.  
  3. # create a file and write it to disk:
  4.  
  5. pathname = (os.getcwd() + "\\test\\") # \\ is required because \ has special meaning to shell
  6. filename = "test.dat"
  7. # create some data:
  8. done = 0
  9. namelist = []
  10. while not done:
  11. name = input("Enter a line:")
  12. print("The last line was: " + name)
  13. if name != "end":
  14. # append name and a line separator (os.linesep = '\n' in windows)
  15. print("if met, should append")
  16. namelist.append(name + os.linesep)
  17. else:
  18. print("else met, should break")
  19. done = 1
  20. print("Bottom of the while")
  21.  
  22. # Create a file object:
  23. # in "write" mode (overwrites any pre-existing filename)
  24. print("opening file")
  25. FILE = open(pathname + filename,"w")
  26.  
  27. # Write all the lines at once:
  28. FILE.writelines(namelist)
  29.  
  30. # Alternatively write them one by one:
  31. for name in namelist:
  32. FILE.write(name)
  33.  
  34. FILE.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement