0

I have been trying to create a program that lets users name, write and save documents, here is what I have come up with so far:

doc_name = str(input("Document Name: "))
end = ""
for line in iter(input, end):
    document = "\n".join(iter(input, end))
    pass
try:
    savefile = open("/home/" +doc_name+ ".txt", "w")
    savefile.write(x)
    savefile.close()
    print("Document - " +doc_name+ "\nSuccessfully saved.\n\n")
except:
    print("An error occurred.\nUnable to save document.\n\n")

The 'for loop' I have used was from the following page: Raw input across multiple lines in Python but I am unsure how to use the input from this loop, so I am able to save it to a textfile. I need the input in this line of code in the place of x:

savefile.write(x)

I am using Python 3.2.3 for this program (if that helps?). I would like to know how the user input entered during the for loop can be stored in a varible and then used at some other point in the program.

Thanks.

9
  • The idention of your code is not valid Python. Please fix it.
    – user1907906
    Commented May 2, 2015 at 15:04
  • @Tichodroma sorry about that, must have had some problems when copying the code over. Thanks for letting me know. Commented May 2, 2015 at 15:08
  • What is for line in iter(input, end): supposed to mean? input is a Python builtin function.
    – user1907906
    Commented May 2, 2015 at 15:10
  • @Tichodroma I have substituted the raw_input from this page stackoverflow.com/questions/11664443/… for input instead. Although, this might not be correct, because I am a beginner when it comes to Python. Commented May 2, 2015 at 15:17
  • i tried the code (after deleting iters) with my python 3.2. albeit it spits error, it actually saves file.
    – marmeladze
    Commented May 2, 2015 at 15:33

1 Answer 1

0
doc_name = input("Document Name: ") # don't need to cast to str
end = ""
result = [] # I recommend initializing a list for the lines
for line in iter(input, end): # you only need this single input call
    result.append(line) # add each line to the list
try:
    # using "with" in this manner is guaranteed to close the file at the end
    with open("/home/" +doc_name+ ".txt", "w") as savefile:
        for line in result: # go through the list of lines
            # write each one, ending with a newline character
            savefile.write(line + '\n')
except IOError:
    print("An error occurred.\nUnable to save document.\n\n")
else: # print this if save succeeded, but it's not something we want to "try"
    print("Document - " +doc_name+ "\nSuccessfully saved.\n\n")

You only need to use pass when Python expects statements (such as in an indented block) but you have no statements for it to execute - it's basically a placeholder. The common use is when you want to define your program's functions (e.g., def myfunction(a, b):) but you don't have the actual content for them yet.

2
  • Thanks for the answer and detailed info, but I've just run the program and instead of writing each line to the textfile, it has written each character on another line. Do you think there is any way to avoid this? Commented May 2, 2015 at 20:28
  • I forgot it was a list. I've edited the code to use append() instead. Commented May 2, 2015 at 20:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.