0

New to Python and having issues with Python seemingly executing code in the background before my print statements are executed.

I am writing a code to parse files and folders on remote file-servers.

f=[]
for key, value in remote_servers.items():
    print(key + '\t' + value)
    if os.path.exists(value):
        deb.debug('listing: ' + value)
        x = [f.name for f in os.scandir(value) if f.is_file()]
        print(x)
    else:
        print("Cannot access '" + value + "'")

It may take several minutes or even hours to parse each remote directory within the dictionary "remote_servers".

It seems the script freezes right at the beginning, parsing the folders in the background first and then spitting out the print statements.

My goal is to know which directory currently script is processing by printing out before and after.

Is there a way to make Python respect the order and print statements before and after a function which may take hours to execute?

StasNamin
  • 1
  • 1
  • Please edit your question to include the code. The comments are not the place for code because there is little formatting options. – Ralf Jul 17 '19 at 11:41
  • I suggest you try to flush the output; [`print()`](https://docs.python.org/3/library/functions.html#print) has an argument `flush` for that. – Ralf Jul 17 '19 at 11:51
  • You can also read this https://stackoverflow.com/questions/230751/how-to-flush-output-of-print-function – Ralf Jul 17 '19 at 11:51
  • Thank you Ralf. Your solution worked. Appreciate it. – StasNamin Jul 17 '19 at 12:06
  • Possible duplicate of [How to flush output of print function?](https://stackoverflow.com/questions/230751/how-to-flush-output-of-print-function) – Corentin Limier Jul 17 '19 at 12:07

0 Answers0