Reputation: 385
Anyone can help me on this task. I'm new to Python. I'm trying to accomplished this: The filename should be hardcode name called: Server_Information.txt and and the second column should be inserted by the user input but the date stamp. Built By : john doe Build Date: %d%m%y Build Reason: Playground Requestor: john doe
Maybe I can use this test script but the first column does not show in the final test file.
Thank you for anyone it helps
from sys import argv
script, filename = argv
print "We're going to erase %r." % filename
print "If you don't want that, hit CTRL-C (^C)."
print "If you do want that, hit RETURN."
raw_input("?")
print "Opening the file..."
target = open(filename, 'w')
print "Truncating the file. Goodbye!"
target.truncate()
print "Now I'm going to ask you for three lines."
line1 = raw_input("Built By : ")
line2 = raw_input("Build Date: %d%m%y ")
line3 = raw_input("Build Reason: ")
line4 = raw_input("Requestor: ")
print "I'm going to write these to the file."
target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")
target.write(line4)
print "And finally, we close it."
target.close()
Upvotes: 1
Views: 140
Reputation: 7255
Since raw_input
returns only the input entered by user, not include the messages you used to prompt, so you need to add those messages to line1
manually, like this:
line1 = "Built By : " + raw_input("Built By : ")
And for line2
I think you want to generate it automatically instead of asking user to enter, you can do it like this:
line2 = "Build Date: " + time.strftime("%d%m%Y", time.localtime())
Upvotes: 0
Reputation: 1024
Try to write this, because now you do not record the invitation as a file.
target.write('%s: %s\n' % ('Built By', line1))
target.write('%s: %s\n' % ('Build Date', line2))
target.write('%s: %s\n' % ('Build Reason', line3))
target.write('%s: %s\n' % ('Requestor', line4))
Upvotes: 0
Reputation: 25
Try closing and reopening the file after the truncate()
target.close()
target = open(filename, 'w')
# ask for user input here
# and close file
Upvotes: 1