Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- # create a file and write it to disk:
- pathname = (os.getcwd() + "\\test\\") # \\ is required because \ has special meaning to shell
- filename = "test.dat"
- # create some data:
- done = 0
- namelist = []
- while not done:
- name = input("Enter a line:")
- print("The last line was: " + name)
- if name != "end":
- # append name and a line separator (os.linesep = '\n' in windows)
- print("if met, should append")
- namelist.append(name + os.linesep)
- else:
- print("else met, should break")
- done = 1
- print("Bottom of the while")
- # Create a file object:
- # in "write" mode (overwrites any pre-existing filename)
- print("opening file")
- FILE = open(pathname + filename,"w")
- # Write all the lines at once:
- FILE.writelines(namelist)
- # Alternatively write them one by one:
- for name in namelist:
- FILE.write(name)
- FILE.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement