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()
Try closing and reopening the file after the truncate()
target.close()
target = open(filename, 'w')
# ask for user input here
# and close file
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))
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())
Related
I'm trying to write the contents of the screen to a text file if the user selects that option. However, Python seems to want to print "Report Complete." to the report.txt file, which I told it to close. I want the "report complete" to display on the screen after it writes to the text file, and move on to the hashing function.
import wmi
import sys
import hashlib
c = wmi.WMI()
USB = "Select * From Win32_USBControllerDevice"
print ("USB Controller Devices:")
for item in c.query(USB):
print (item.Dependent.Caption)
print (" ")
print ("======================================")
report = input ("Would you like the results exported to a file? ")
if (report) == "yes":
file = open('report.txt', 'w')
sys.stdout = file
for item in c.query(USB):
print (item.Dependent.Caption)
file.close()
print ("Report complete.")
else:
print ("Job Complete.")
hashInput = input ("Would you like to hash the report? ")
if (hashInput) == "yes":
hash = hashlib.md5(open('report.txt', 'rb').read()).hexdigest()
print ("The MD5 hash value is:", (hash))
else:
print ("Job Complete.")
You set sys.stdout to a file, and then closed the file. That makes everything you try and print, which would normally go to stdout, try and go to a closed file. If I may suggest, don't reassign sys.stdout. It is not necessary.
if report == "yes":
with open('report.txt', 'w') as fout:
for item in c.query(USB):
print(item.Dependent.Caption, file=fout)
print("Report complete.")
I'm completing exercise 16 from Learn Python the Hard Way and it's asking this question:
Write a script similar to the last exercise that uses read and argv to
read the file you just created.
I'm attempting to use the 'read' function in order to have the script automatically run and display the text file that the script creates. But nothing shows up when I run everything, it's just an extra blank space before "close it". How do I get it to display anything?
from sys import argv
script, filename = argv
txt = open(filename)
print "Erase %r" % filename
print "hit CTRL-C (^C)."
print "hit RETURN."
raw_input("?")
print "Opening the file..."
target = open(filename, 'w')
print "Truncating the file."
target.truncate()
print "Need 3 lines."
line1 = raw_input("line 1: ")
line2 = raw_input("line 2: ")
line3 = raw_input("line 3: ")
print "Write these to a file"
target.write("{0}\n{1}\n{2}\n".format(line1, line2, line3))
print txt.read()
print "Close it."
target.close()
You should close the file after writing and then open it again for reading:
from sys import argv
script, filename = argv
print "Erase %r" % filename
print "hit CTRL-C (^C)."
print "hit RETURN."
raw_input("?")
print "Opening the file..."
with open(filename, 'w') as target:
print "Truncating the file."
target.truncate()
print "Need 3 lines."
line1 = raw_input("line 1: ")
line2 = raw_input("line 2: ")
line3 = raw_input("line 3: ")
print "Write these to a file"
target.write("{0}\n{1}\n{2}\n".format(line1, line2, line3))
txt = open(filename)
print txt.read()
print "Close it."
target.close()
The second version (without "with as" structure):
from sys import argv
script, filename = argv
print "Erase %r" % filename
print "hit CTRL-C (^C)."
print "hit RETURN."
raw_input("?")
print "Opening the file..."
target = open(filename, 'w')
print "Truncating the file."
target.truncate()
print "Need 3 lines."
line1 = raw_input("line 1: ")
line2 = raw_input("line 2: ")
line3 = raw_input("line 3: ")
print "Write these to a file"
target.write("{0}\n{1}\n{2}\n".format(line1, line2, line3))
print "Close it."
target.close()
txt = open(filename)
print txt.read()
I can't figure out how to write the user input to an existing file. The file already contains a series of letters and is called corpus.txt . I want to take the user input and add it to the file , save and close the loop.
This is the code I have :
if user_input == "q":
def write_corpus_to_file(mycorpus,myfile):
fd = open(myfile,"w")
input = raw_input("user input")
fd.write(input)
print "Writing corpus to file: ", myfile
print "Goodbye"
break
Any suggestions?
The user info code is :
def segment_sequence(corpus, letter1, letter2, letter3):
one_to_two = corpus.count(letter1+letter2)/corpus.count(letter1)
two_to_three = corpus.count(letter2+letter3)/corpus.count(letter2)
print "Here is the proposed word boundary given the training corpus:"
if one_to_two < two_to_three:
print "The proposed end of one word: %r " % target[0]
print "The proposed beginning of the new word: %r" % (target[1] + target[2])
else:
print "The proposed end of one word: %r " % (target[0] + target[1])
print "The proposed beginning of the new word: %r" % target[2]
I also tried this :
f = open(myfile, 'w')
mycorpus = ''.join(corpus)
f.write(mycorpus)
f.close()
Because I want the user input to be added to the file and not deleting what is already there, but nothing works.
Please help!
Open the file in append mode by using "a" as the mode.
For example:
f = open("path", "a")
Then write to the file and the text should be appended to the end of the file.
That code example works for me:
#!/usr/bin/env python
def write_corpus_to_file(mycorpus, myfile):
with open(myfile, "a") as dstFile:
dstFile.write(mycorpus)
write_corpus_to_file("test", "./test.tmp")
The "with open as" is a convenient way in python to open a file, do something with it while within the block defined by the "with" and let Python handles the rest once you exit it (like, for example, closing the file).
If you want to write the input from the user, you can replace mycorpus with your input (I am not too sure what you want to do from your code snippets).
Note that no carriage return is added by the write method. You probably want to append a "\n" at the end :-)
#I wrote the following code for making a text editor.
print "This is a simple text editor"
from sys import argv
script, name = argv
print "You have selected the file : %s" %name
print "Opening the file...."
t = open(name, 'r+')
print "The contents of the file are"
print t.read()
f = open(name, 'w+')
print "Now we will truncate the file and empty it of it's contents"
f.truncate()
print "Now let us write something into our file\n"
x = raw_input('What do you want to write\n') #Works fine till here
f.write(x)
print "Now we read our file again"
print f.read()
print "And finally we close the file"
f.close()
After the promp to write something in the file, the script goes awry and produces strange symbols instead of the typed text. Please help
You need to close and re-open your file.
print "This is a simple text editor"
from sys import argv
script, name = argv
print "You have selected the file : %s" %name
print "Opening the file...."
t = open(name, 'r+')
print "The contents of the file are"
print t.read()
t.close() ##########
f = open(name, 'w+')
print "Now we will truncate the file and empty it of it's contents"
f.truncate()
print "Now let us write something into our file\n"
x = raw_input('What do you want to write\n') #Works fine till here
f.write(x)
f.close() ##########
f = open(name, 'r+') ##########
print "Now we read our file again"
print f.read()
print "And finally we close the file"
f.close()
I'm trying to write a program similar to this guy's Learn Python the Hard Way program, near the top of the page.
http://learnpythonthehardway.org/book/ex16.html
This is my version below. But it tells me off for using "%r" at the end, why does it do that? I thought that's what you're meant to do in parenthesis.
# -- coding: utf-8 --
from sys import argv
script, filename = argv
print "Would you like file %r to be overwritten?" % filename
print "Press RETURN if you do, and CTRL-C otherwise."
raw_input('> ')
print "Opening the file ..."
target = open(filename, 'w')
target.truncate()
print "Now type three lines to replace the contents of %r" % filename
line1 = raw_input("line 1: ")
line2 = raw_input("line 2: ")
line3 = raw_input("line 3: ")
print "The lines below have now overwritten the previous contests."
target.write("%r\n%r\n%r") % (line1, line2, line3)
target.close()
You need to place the % operator directly after the format string. Take the parenthesis here:
target.write("%r\n%r\n%r") % (line1, line2, line3)
# --^
And move it to the end of the line:
target.write("%r\n%r\n%r" % (line1, line2, line3))
# --^
Also, I would like to mention that performing string formatting operations with % is frowned upon these days. The modern approach is to use str.format:
target.write("{0!r}\n{1!r}\n{2!r}".format(line1, line2, line3))