0

Was using Python shutil.move() method but need to move million files from one location to another and I would like to redirect / silent the output of shutil.move().

if os.path.exists(logs_path_archive):
    for source_file in downloaded_files:
        destination_file = source_file.replace(logs_path,logs_path_archive)
        shutil.move(source_file,destination_file)

Any recommendation?

Luis R. Gonzalez
  • 358
  • 3
  • 16

1 Answers1

0

In your comment you specify, you are using a Jupyter Notebooks. So you can use a couple of options built into IPython/Jupyter for that.

If the move step is just one cell, at the start of the cell put the following:

%%capture out_stream
shutil.move(<original_filename>,<new_file_name>)

However, it sounds like the move step is in amongst many other steps in a cell, and so to just hush the move, you can use the with io.capture_output() as captured: to suppress output from only what is in the with block, like so:

from IPython.utils import io
with io.capture_output() as captured:
    shutil.move(<original_filename>,<new_file_name>)

See the page here for more about both of those. The %%capture cell magic will probably be above where that link sends you.


If you do need to use pure python you can use a with statement and contextlib (like a contextmanager to handle output) to redirect that, similar to the with io.capture_output() as captured idea above. Here I'm sending it to _ which is often used when you don't plan to use the object assigned later.

import contextlib
with contextlib.redirect_stdout(_):
    shutil.move(<original_filename>,<new_file_name>)
Wayne
  • 6,607
  • 8
  • 36
  • 93