-1

I already read some useful infos here about moving files with Python. But those are not working on my machine. I use eclipse to run python and the program should move files within windows. I used os.rename, shutil.move, shutil.copy and so on....

Here is my simple code.

import os
import shutil

source = os.listdir("C:/test/")
dest_dkfrontend = "C:/test1/"
for files in source:
    if files.startswith("Detail"):
        print('Files found ' + files)
        shutil.copy(files, dest_dkfrontend)
    else:
        print('File name not matching')

I receive an error like:

with open(src, 'rb') as fsrc:
IOError: [Errno 2] No such file or directory:

Could you please help to address this?

tobias_k
  • 81,265
  • 12
  • 120
  • 179

1 Answers1

0

first you have to check if your destination directory exists or not.
and shutil.copy requires 1st parameter as source of file with file name and 2nd parameter as destination of file where to copy with new file name.

try this.

import os
import shutil

source = os.listdir("C:/test/")
dest_dkfrontend = "C:/test1/"

if not os.path.exists(dest_dkfrontend):
    os.makedirs(dest_dkfrontend)

for files in source:
    if files.startswith("Detail"):
        print('Files found ' + files)
        shutil.copy(source+files, dest_dkfrontend+files)
    else:
        print('File name not matching')
singhiskng
  • 511
  • 1
  • 5
  • 15