0

I know there are many other ways to solve a problem like this but i would love to know why this code isn't working..

import shutil
import os  # Access to some functions of the operating system
import time

def auto_backup():
    source_dir = r'C:\Users\Derma\OneDrive\Desktop\back'
    target_dir = r'C:\Users\Derma\OneDrive\Desktop\test'

    file_names = os.listdir(source_dir)
    while True:
        for file_name in file_names:
            shutil.move(os.path.join(source_dir, file_name), target_dir)
            time.sleep(1)
            print("Executed")

auto_backup()

The problem is: The program does the shutil command but doesn't execute the print function call. Why?

martineau
  • 119,623
  • 25
  • 170
  • 301
IIIIII
  • 3
  • 3
  • 1
    You need to flush the output buffer if you want to see print results immediately - [How can I flush the output of the print function (unbuffer python output)?](https://stackoverflow.com/questions/230751/how-can-i-flush-the-output-of-the-print-function-unbuffer-python-output) – shriakhilc Apr 07 '22 at 23:18
  • Assuming `file_names` isn't empty, the print _must_ be happening. How are you running this code? Are you running it with `python myscript.py` on the command line, or are you using an IDE, or something else? – John Gordon Apr 07 '22 at 23:19
  • Right -- if you are using an IDE or redirecting the output using `>`, then buffering is definitely the problem. The output is being produced, but not being shown. – Tim Roberts Apr 07 '22 at 23:20
  • @shriakhilc stdout is line-buffered by default, so I'm not sure flushing will help. – John Gordon Apr 07 '22 at 23:20
  • Thanks for both answers, i think it was a mix of both.. – IIIIII Apr 07 '22 at 23:22
  • 3
    Either post the solution as an answer below or delete the question. Don't replace the question text with "solved", that's useless. – Barmar Apr 07 '22 at 23:27
  • 2
    Please don't vandalize your posts. By posting on the Stack Exchange network, you've granted a non-revocable right, under the [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/) license, for Stack Exchange to distribute that content (i.e. regardless of your future choices). By Stack Exchange policy, the non-vandalized version of the post is the one which is distributed, and thus, any vandalism will be reverted. If you want to know more about deleting a post please see: [How does deleting work?](https://stackoverflow.com/help/what-to-do-instead-of-deleting-question). – jmoerdyk Apr 07 '22 at 23:30
  • No one has mentioned that fact that the `auto_backup()` function in your question contains an infinite loop and will never return… – martineau Apr 07 '22 at 23:49
  • 1
    @martineau nor the fact that `shutil.move` is a bad way to do a backup. – Mark Ransom Apr 08 '22 at 01:25
  • @MarkRansom which way would you recommend? Thanks for the answer. – IIIIII Apr 09 '22 at 20:17
  • Well `move` will generally delete the original when it's done, which is why I called it a bad choice. `copy` would almost certainly be better. – Mark Ransom Apr 10 '22 at 01:22

0 Answers0