brohjoe
brohjoe

Reputation: 934

Python shutil.copy2() function throws "No such file" error

I've written a simple script to search for a file type in one directory and copy and move it to another directory. The script will print the first searched file in the source directory but when it gets inside the for loop it throws a FileNotFoundError. Can someone look at this and tell me what I'm doing wrong? I've used single slashes in the path, forward and backward slashes and now I'm using escaped slashes with an r in the front of the path and it does not matter, it still gives an error.

import os
import fnmatch
import shutil

src = (r"C:\\Users\\myName\\Documents\\development")
dst = (r"C:\\ProgramData\\MySQL\\MySQL Server 8.0\\Data\\myData")

print(os.listdir(src)) #this will display everything in the src directory.


for file_name in os.listdir(src):
 if fnmatch.fnmatch(file_name, "*.ibd"):
    print(file_name)   #This will only print the first searched for file type.
    shutil.copy2(file_name, dst) #if I comment out this line, it will print all .ibd files.

Upvotes: 1

Views: 1213

Answers (1)

brohjoe
brohjoe

Reputation: 934

Thanks furas for your help. I had to understand what you were saying and how it fits into the code structure, but I figured it out. Thank you so much for your assistance. I hope this helps someone else.

import os
import fnmatch
import shutil

src = (r"C:\\Users\\myName\\Documents\\development")
dst = (r"C:\\ProgramData\\MySQL\\MySQL Server 8.0\\Data\\myData")

for file_name in os.listdir(src):
    if fnmatch.fnmatch(file_name, "*.ibd"):
        print(file_name)
        shutil.copy2(os.path.join(src, file_name), dst)

Upvotes: 1

Related Questions