stdout

redirect stdout to tkinter text widget

删除回忆录丶 提交于 2019-11-29 11:59:37
I'm trying to redirect the stdout of a function to a tkinter text widget. The problem I am running into is that it writes each line to a new window instead of listing everything in one. The function scans a directory and lists any file that is 0k. If no files are 0k it prints that. So, the problem is that if there are 30 0k files in a directory, it will open 30 windows with a single line in each. Now, I know what the problem is. If you look in my function code Zerok() I am telling it: if os.stat(filename).st_size==0: redirector(filename) I know that every time os.stat sees a file that is 0k it

using bash: write bit representation of integer to file

好久不见. 提交于 2019-11-29 11:54:41
问题 I have a file with binary data and I need to replace a few bytes in a certain position. I've come up with the following to direct bash to the offset and show me that it found the place I want: dd bs=1 if=file iseek=24 conv=block cbs=2 | hexdump Now, to use "file" as the output: echo anInteger | dd bs=1 of=hextest.txt oseek=24 conv=block cbs=2 This seems to work just fine, I can review the changes made in a hex editor. Problem is, "anInteger" will be written as the ASCII representation of that

What do double parentheses mean in a function call? e.g. func(stuff)(stuff)?

最后都变了- 提交于 2019-11-29 11:49:12
问题 Original title: " Help me understand this weird Python idiom? sys.stdout = codecs.getwriter('utf-8')(sys.stdout) " I use this idiom all the time to print a bunch of content to standard out in utf-8 in Python 2.*: sys.stdout = codecs.getwriter('utf-8')(sys.stdout) But to be honest, I have no idea what the (sys.stdout) is doing. It sort of reminds me of a Javascript closure or something. But I don't know how to look up this idiom in the Python docs. Can any of you fine folks explain what's

Redirect stdout and stderr to the same file and restore it

有些话、适合烂在心里 提交于 2019-11-29 11:39:09
问题 I am redirecting the output of stderr and stdout of my c program to two files and then restoring the original stdout and stderr: int sout = dup(fileno(stdout)); freopen("test.txt","w",stdout); int serr = dup(fileno(stderr)); freopen("test.txt","a",stderr); //some output.... dup2(sout,fileno(stdout)); close(sout); dup2(serr,fileno(stderr)); close(serr); That's the code axample. This works. But I would like to redirect stdout and stderr to the same file(and later restore it again) so that the

Why doesn't python3's print statement flush output when end keyword is specified?

两盒软妹~` 提交于 2019-11-29 11:37:01
from sys import argv, stdout as cout from time import sleep as sl print("Rewinding.......",end = '') # If end is given output isn't flushed. But why? cout.flush() for i in range(0,20): sl(0.2) print(".",end='',flush = True) #No output is printed if flush wasn't specified as true. print("Done") #Output is by default flushed here When I specified end and sleep, I noticed that output wasn't flushed until next print where it was by default flushed. Why does this happen? I had to manually flush the output. In fact this is the default behavior of the underlying stdio functions. When the output is to

How can I display the output of a Opscode Chef bash command in my console?

江枫思渺然 提交于 2019-11-29 11:31:50
问题 I use Vagrant to spawn a standard "precise32" box and provision it with Chef so I can test my Node.js code on Linux when I work on a Windows machine. This works fine. I also have this bash command so it auto installs my npm modules: bash "install npm modules" do code <<-EOH su -l vagrant -c "cd /vagrant && npm install" EOH end This also works fine except that I never see the console output if it completes successfully. But I'd like to see it so we can visually monitor what is going on. This

passing \\n (new line) on stdout throught sys argument

谁说胖子不能爱 提交于 2019-11-29 11:14:22
This is elementary I guess: Let's consider this snippet: for i in range(3): sys.stdout.write(str(i) + '\n') out: 0 1 2 and this: for i in range(3): sys.stdout.write(str(i) + sys.argv[1]) out (after passing \n as argument): 0\n1\n2\n So, how can I pass new-line as argument? sys.stdout.write(str(i) + sys.argv[1].decode("string_escape")) 来源: https://stackoverflow.com/questions/5715414/passing-n-new-line-on-stdout-throught-sys-argument

Nohup and Python -u : it still doesn't log data in realtime

与世无争的帅哥 提交于 2019-11-29 11:03:20
When launching a Python process, in background, with nohup python myscript.py > test.log 2>&1 < /dev/null & the problem is that stdout is buffered: the data is not written in realtime to test.log . The common solution to this problem is to flush periodically with sys.stdout.flush() , or even better, as suggested by this answer , to use python -u : nohup python -u myscript.py > test.log 2>&1 < /dev/null & But this is not enough . I noticed that it worked during a few hours, and then, it stopped working, i.e. after a few hours test.log is not written in realtime anymore, even if I used nohup

redirect output from stdout to a string?

你离开我真会死。 提交于 2019-11-29 10:51:52
in C i want to redirect the output of a process from stdout to write to a "shared memory segment" which can be thought of as a char array or a string with a pointer i know that there is dup2 but it takes file discriptors as argument not a pointer to an array. is there any way to redirect it to a string? char string[SIZE]; freopen("/dev/null", "a", stdout); setbuf(stdout, string); see freopen and setbuf for their definitions This should work on UNIX systems: // set buffer size, SIZE SIZE = 255; char buffer[SIZE]; freopen("/dev/null", "a", stdout); setbuf(stdout, buffer); printf("This will be

python capture print output of another module

爷,独闯天下 提交于 2019-11-29 10:16:06
I was wondering if this is possible in python. #module1 def test(): print 'hey' #module2 import module1 # *Without modifying module1* is there anyway to wrap this in module2 so that I can capture the # print 'hey' inside a variable? apart from running module1 as a script? module1.test() # prints to stdout Thanks! Yes, you can. You need to take control of sys.stdout . Something like this: import sys stdout_ = sys.stdout #Keep track of the previous value. sys.stdout = open('myoutputfile.txt', 'w') # Something here that provides a write method. # calls to print, ie import module1 sys.stdout =