Search code examples
pythonuser-interfacecmdcx-freezexcopy

Copying Files Silently with xcopy - Python Windows 10


I have a quick question involving Copying files.When copying files in a script I am using the following:

os.system('echo f | xcopy /f /y "{source}" "{target}"'.format(source=source, target=target))

I use this because it is MUCH faster than shutil.copyfile(source, target). This works fine, but it prints out this ugly prompt string in the console for each file copied (see image below).

enter image description here

And if I compile the program to an exe file (using cx_Freeze) without the CMD window being open for a GUI, it rapidly opens and closes a CMD window when each file is copying (like a flicker). So if I am copying 30 files, a CMD window flickers open and closed 30 times.

Is there any way to silence this? Maybe through a flag or something? Or possibly some other method entirely that I could use that is as equally as efficient as xcopy without the annoying prompt and flickering windows?

Thanks in advance.


Solution

  • Try:

    import os
    os.system('echo f | xcopy /y "{source}" "{target}" > nul'.format(source=filename, target=filename_txt))
    

    Works fine in my machine.