Python pipe command output to file




















Great answer, if you want a one-liner you can do something like echo gday xargs python -c "import sys; print sys. Alex Thomas Alex Thomas 10 10 silver badges 13 13 bronze badges.

Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Making Agile work for data science. Stack Gives Back Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually.

Linked 0. Related Hot Network Questions. Question feed. Stack Overflow works best with JavaScript enabled. Write Python stdout to file immediately Ask Question. Asked 6 years, 11 months ago. Active 1 year, 7 months ago. Viewed k times. For example: script. Improve this question. Bart Bart 1 1 gold badge 7 7 silver badges 9 9 bronze badges. Add a comment. Active Oldest Votes. Improve this answer. Community Bot 1. Digital Trauma Digital Trauma 7, 2 2 gold badges 21 21 silver badges 36 36 bronze badges.

Thanks, I didn't know about the buffering! Knowing that, Google pretty quickly told me that python -u script. EDIT So many answers at once, I accepted yours since it pointed me in the direction of the buffering. Some command-line programs also have similar options - e. DigitalTrauma: Isn't it better to use no buffering at all i. However if it is absolutely necessary to see each character as it is output then yes, -o0 would be required.

Paul Please avoid copy pasting contents between answers, or at the bery least mention the original authors that provided the content. Show 3 more comments. This should do the job: import time, sys for i in range 10 : print 'bla' sys. So, the following will do too: python -u script. Sergiy Kolodyazhnyy Sergiy Kolodyazhnyy 15k 8 8 gold badges 43 43 silver badges 92 92 bronze badges. And the documentation for sys 's strems says: When interactive, standard streams are line-buffered.

Bakuriu Bakuriu 8 8 silver badges 22 22 bronze badges. Python 2. DEBUG or any level you want logger. Hi, I want to use this logging for writing the console data to the log file with the time like at which time that data is taken.

But i am not able to understand the logging function or library properly. Can you help me with this — haris. If you are still unable to get it to work, ask a new question. So before running. Community Bot 1 1 1 silver badge. Aaron Dufour Aaron Dufour The question specifically wants to pipe the output of print to a file.

It would be reasonable to expect stdout stack traces and the like to still print to the terminal. He said it wasn't working, mine wasn't working either. I later discovered that this app I'm working on was configured to direct everything to stderr With disk caching performance of the original should be acceptable. This solution however has the drawback of ballooning the memory requirements if there were a lot of output.

Though probably nothing to worry about here, it is generally a good idea to avoid this if possible. Same idea as using xrange py3 range instead of range, etc. Gringo: He didn't specify this requirement. Rarely do I ever write enough data to a file that this would be relevant. Disk caching might help but it's still a bad practice to keep a file handle open for a large body of code. Your comment contradicts itself. To be honest the performance aspect of both approaches is irrelevant for non-huge amounts of data.

Perhaps a generator vs list is a better example though. Gringo: I fail to see how my comment contradicts itself. Maybe the performance aspect isn't relevant, keeping a file handle open for an extended period always increases the risk of error. The shorter you have a file open for, the better, simply because you don't control the file system from your code. Gringo: I appreciate your criticism and enjoyed the heated debate.

Even though we disagreed on some points I still respect your views as it's clear you have a good reason for taking your stance.

Thanks for ending it reasonably and have a very good night. Show 3 more comments. Graham Graham 2, 1 1 gold badge 14 14 silver badges 28 28 bronze badges. Good idea. This context manager exists in the contextlib module however, docs say since Python 3. GringoSuave Yep, the contextmanager function should be imported to use as a decorator; hence why I put from contextlib import contextmanager at the top of the code block.

Haha this is a nice trick, do you use this kind of trick all the time? Your bug is somewhere else: it could be in the code you removed for your question where does filename come from for the call to open? Jerome Jerome 2, 14 14 silver badges 23 23 bronze badges. In python 3, you can reassign print :! Shawn Shawn 5 5 silver badges 10 10 bronze badges. The Overflow Blog. Podcast Making Agile work for data science. Stack Gives Back Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually.

Visit chat. Linked 0. See more linked questions. Related Hot Network Questions. Question feed.



0コメント

  • 1000 / 1000