1

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()

3 Answers 3

1

Try closing and reopening the file after the truncate()

 target.close()
 target = open(filename, 'w')
 # ask for user input here
 # and close file
Sign up to request clarification or add additional context in comments.

Comments

0

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))

1 Comment

Same here I replacing my target.write with yours and is not working is giving me error.
0

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())

2 Comments

WKplus, Thank you for you input. I cannot get it to work. I'm getting error name not defined. I run in a debugger mode. python -m pdb script.py but as son I put the name of the file it dies.
@pirulo You need to add import time before you use time.strftime .

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.