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.

Community
  • 1
  • 1
BaconStereo
  • 19
  • 1
  • 7
  • The idention of your code is not valid Python. Please fix it. –  May 02 '15 at 15:04
  • @Tichodroma sorry about that, must have had some problems when copying the code over. Thanks for letting me know. – BaconStereo May 02 '15 at 15:08
  • What is `for line in iter(input, end):` supposed to mean? `input` is a Python builtin function. –  May 02 '15 at 15:10
  • @Tichodroma I have substituted the `raw_input` from this page http://stackoverflow.com/questions/11664443/raw-input-across-multiple-lines-in-python for `input` instead. Although, this might not be correct, because I am a beginner when it comes to Python. – BaconStereo May 02 '15 at 15:17
  • i tried the code (after deleting iters) with my python 3.2. albeit it spits error, it actually saves file. – marmeladze May 02 '15 at 15:33
  • @marmeladze did it let you input before giving an error? When I removed the `iter`s, it wouldn't let me input and didn't save the file either. For this code: `document = "\n".join(input, end)`, I got this error: `TypeError: join() takes exactly one argument (2 given)` and when putting another set of brackets over `((input, end))`, it gave the error: `TypeError: sequence item 0: expected str instance, builtin_function_or_method found`. – BaconStereo May 02 '15 at 15:46
  • here is the code, i've tried http://ur1.ca/ka7yq – marmeladze May 02 '15 at 17:37
  • @marmeladze thanks for trying that out, but I'm not sure what the random numbers are for? Also, that program doesn't let the user input on more than one line. – BaconStereo May 02 '15 at 18:15
  • i just put it, for files not being empty :) not anything more. – marmeladze May 02 '15 at 18:49
  • @marmeladze oh ok, thanks :) – BaconStereo May 02 '15 at 19:29

1 Answers1

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.

TigerhawkT3
  • 46,954
  • 6
  • 53
  • 87