-1

I am trying to move the .csv files from one folder to another once the code is completed. Tried using multiple solutions like shutil.move and os.replace and also one of the solutions mentioned in the previous closed posts.

Here is my recent solution:

src = "C:\\Manual Upload\\PY Upload\\CC\\"
dest = "C:\Manual Upload\PY Upload"


for f in Path(src).glob("*.csv"):
    shutil.move(os.path.join(src, f), dest)

This copies the file to my destination folder, but does not remove it from source and also gives the following error:

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Manual Upload\\PY Upload\\CC\\QA Finance GRM CONS ASPAC_Cost Center_6698_WI4_2021_CC.csv'

I want to delete the .csv from source and paste it to destination.

Miles Morales
  • 248
  • 1
  • 5
  • 20
  • 1
    The CSV isn't open in Excel or some other viewer, is it? – Zulfe Feb 02 '22 at 18:23
  • Also make sure that you're closing the file after you `open(...)` it- I think python will try to open a second (separate) handle to implement `shutil.move(...)`, and if the original hasn't been `.close()`d... – Dillon Davis Feb 02 '22 at 18:25
  • @Zulfe my bad, it was opened, this code works now. But one problem, If I again paste the same csv in sources, it says file already exists in dest. And also, how to check if csv is open or not? – Himanshu Pathak Feb 02 '22 at 18:28
  • @HimanshuPathak Take a look at [this](https://stackoverflow.com/questions/7419665/python-move-and-overwrite-files-and-folders) thread for overwriting the file in the destination if it already exists. Taking a look at [this](https://stackoverflow.com/questions/11114492/check-if-a-file-is-not-open-nor-being-used-by-another-process) thread for checking for a file lock before attempting to use it. – Zulfe Feb 02 '22 at 18:34

1 Answers1

0

This will copy files from one source directory to another one by one

import os
import shutil

src = "C:\\Manual Upload\\PY Upload\\CC\\"
dest = "C:\\Manual Upload\\PY Upload\\"

for item in os.listdir(src):
    print(item)
    shutil.copyfile(f"{src}{item}", f"{dest}{item}")

credit: How to copy files?

Andrew Ryan
  • 1,489
  • 3
  • 15
  • 21