0

I am running a python script which checks for the modifications of files in a folder. I want that output to be printed in a file. The problem is that the output is DYNAMIC , the cmd is always open and when a file is modified, I will have an information right-ahead about that in the cmd window. All the solutions which I found were matching the situations were I just run a command and I finish with that.

I tryed with:
python script.py > d:\output.txt
but the output.txt file is empty

An example of the command prompt windows, after I run the command python script.py and I touch the 2 files, the command prompt will look like this. I want to capture that output. enter image description here

Solution: In the python script which I use, add to the logging.basicConfig function, one more argument : filename='d:\test.log'

John Doe
  • 1,058
  • 8
  • 30
  • Have you tried `python script.py > d:\\output.txt` – mkaran Aug 11 '16 at 12:31
  • Yes I did, same errors – John Doe Aug 11 '16 at 12:32
  • What are the "weird errors" you mention? – glibdud Aug 11 '16 at 12:35
  • I assume d is the correct drive? Does it work in c? (Dumb question but sometimes...). It would be good to know what the code in script.py is though.. – mkaran Aug 11 '16 at 12:35
  • @glibdud I added the screenshot and I also managed to get rid of the error by writting a single `>` instead of `>>`, but the output file is empty – John Doe Aug 11 '16 at 12:38
  • @mkaran it is dump question, won't make sense. – U.Swap Aug 11 '16 at 12:39
  • @John Doe look into the error carefully, possibly the last line , it's clearly saying "Can't find the file specified" there's nothing so weird about it! – U.Swap Aug 11 '16 at 12:39
  • I got rid of the error, but the output file is still empty – John Doe Aug 11 '16 at 12:41
  • does the script actually print something? What is the expected content of output.txt ? Do you mean that you need to see the output.txt updating while the script is running? Because that would be a different case than just > output.txt ,... – mkaran Aug 11 '16 at 12:43
  • I added an example with how the script works and yes, I want it updated while it's running, this is why the duplicate suggestion from @U.Swap is a non-sense – John Doe Aug 11 '16 at 12:48

3 Answers3

1

The issue is output buffering. If you wait long enough, you'll eventually see data show up in the file in "blocks". There are a few ways around it, for example:

  • Run python with the -u (unbuffered) flag
  • Add a sys.stdout.flush() after all print statements (which can be simplified by replacing stdout with a custom class to do it for you; see the linked question for more)
  • Add flush=True option to print statements if your version of Python supports it
  • If appropriate, use the logging module instead of print statements.
Community
  • 1
  • 1
glibdud
  • 7,550
  • 4
  • 27
  • 37
  • I don't have acces to the "print statements". I use the watchdog module and all the printing is done inside it's functions. Do you suggest to modify the watchdog code? – John Doe Aug 11 '16 at 13:19
  • @JohnDoe That would have been useful information to add to your question. If [this](https://pypi.python.org/pypi/watchdog) is the watchdog module you're using, it looks like they've integrated with the `logging` module. You may just need to tell it to use a file instead of stdout. I'd suggest playing with the [`logging.basicConfig()`](https://docs.python.org/2.7/library/logging.html#logging.basicConfig) call as in the example on their page. – glibdud Aug 11 '16 at 13:26
  • @JohnDoe Specifically, add a `filename` argument to it. – glibdud Aug 11 '16 at 13:31
  • @JohnDoe For future reference, this is why it's recommended to include an example of actual code that exhibits the behavior you're trying to correct. – glibdud Aug 11 '16 at 13:33
  • Thank you for the answer, I will be more specific on my future questions! I will mark this as an answer :) – John Doe Aug 11 '16 at 13:36
0

python test.py>test.txt

It's working for me in windows cmd prompt

  • Because you have a one run command, my command prompt window is always open and has a dynamic output(it's constantly updating while it's open!!! – John Doe Aug 11 '16 at 12:44
0

As I see it the simplest would be to add the file handling (the writing to output.txt ) inside your script. Thus, when it is time to print the information you need to have (as your example shows when you touch two files you print two lines), you can open the file, write the specific line and close it after it is done (then you can see the updated output.txt). Get the file path for the output.txt as a command line argument like

python script.py --o 'd:\output.txt'

for example.

mkaran
  • 2,528
  • 20
  • 23
  • The problem with that is: I work with the watchdog Observer and LoggingEventHandler module. So the output is given inside it's functions, I don't have acces to it. This is why I wanted to do it with the `>` cmd way – John Doe Aug 11 '16 at 12:58