मेरे पास एक कोड है जो कोड में कुछ प्रक्रियाओं और कुछ प्रिंटों को नियंत्रित करने के लिए pexpect
का उपयोग करता है। मुख्य लक्ष्य (इस प्रश्न में) pexpect
आउटपुट और कुछ लॉग फ़ाइल में लॉग इन प्रिंट करना है। जिस समस्या में मैंने भाग लिया वह यह है कि pexpect
लाइनें (भेजा और प्राप्त डेटा) प्रिंट के साथ कोई स्पष्ट तर्क नहीं मिला है। मैं उम्मीद कर रहा था कि प्रिंट स्ट्रिंग और pexpect
आउटपुट जारी किए गए क्रम में लॉग इन होंगे।प्रिंट और pexpect लॉगिंग
नमूना कोड निम्नलिखित है:
#!/usr/bin/env python
import pexpect
import time, sys, os
###############################################################################
# Subclass of file object to avoid recording extensive whitespace characters
class CleanFile(file):
def write (self, text):
# Remove the whitespaces
out_text = ''
# process the backspace properly
bline = ''
for c in text:
if (ord(c) == 0x8):
if (len(bline) == 0):
# Move the file pointer.
file.seek(self, -1, os.SEEK_CUR);
else:
bline = bline[:-1]
else:
bline += c
# remove whitespaces from inside a line
out_text += ''.join(c for c in bline if (ord(c) >= 32 or ord(c) == 10));
file.write(self, out_text);
###############################################################################
def main():
fout = CleanFile ("options.log_file.log", 'w')
sys.stdout = os.fdopen (sys.stdout.fileno(), 'w', 0)
os.dup2 (fout.fileno(), sys.stdout.fileno());
p = pexpect.spawn ('tclsh')
p.logfile = fout
print "Got into tclsh."
p.sendline('ls');
p.expect (['%',pexpect.EOF])
p.sendline('info tclversion');
p.expect (['%',pexpect.EOF])
print "Got the version\n"
p.sendline('info commands %');
p.expect (['%',pexpect.EOF])
p.sendline('exit');
print 'Ended session'
###############################################################################
if __name__ == "__main__":
main()
यह आउटपुट लॉग फ़ाइल सामग्री है:
Got into tclsh.
ls
% lsinfo tclversion
log options.log_file.log pexpect_test.py runtests.py runtests_steinway.py
% info tclversionGot the version
info commands %
8.4
% info commands %exit
Ended session
वहाँ pexpect
और प्रिंट आउटपुट अनुक्रमिक बनाने के लिए कोई तरीका है?
अद्यतन: pexpect
manual page के आधार पर: "कृपया ध्यान दें, तथापि, कि बफरिंग इस व्यवहार के बाद से इनपुट अप्रत्याशित मात्रा में आता है प्रभावित कर सकते हैं"। तो यह लॉगिंग को संभावित रूप से प्रभावित कर सकता है।
अच्छा विचार है। क्या अभी भी तुरंत काम छापेगा? इसे तब 'प्रिंट' विधि द्वारा नियंत्रित किया जाना चाहिए। मेरे पास एक सवाल है: क्या पी। पहले + पी। मैच + पी। बाद में इस्तेमाल किया जाना चाहिए? – ilya1725
तुरंत प्रिंटिंग काम नहीं करती है क्योंकि डेटा आपकी मूल लिपि में असीमित रूप से वापस आ जाता है। पी। इससे मेल खाने वाली स्ट्रिंग से पहले सब कुछ लौटाता है। चूंकि मिलान की गई स्ट्रिंग प्रॉम्प्ट है, इसलिए सभी कमांड आउटपुट पहले से ही आ चुके हैं। – Edu