Alanlyyy
Alanlyyy

Reputation: 67

Shutil.copytree ----> WindowsError: [Error 3] The system cannot find the path specified:

I am trying to extract contents from source and move into destination folder.

folder1 = 2018
folder2 = 8    
folder3 = 3
    source = os.path.join("C:\\","Pizza","Sammy","Logs", "Archive", "DataLog_Private" ,str(folder1),"0" + str(folder2),"0" + str(folder3))

    destination = os.path.join("C:\\","Users", "alex", "Desktop", "logPull" , "DataLog_Private" ,str(folder1),"0" + str(folder2),"0" + str(folder3))

shutil.copytree(source,destination)

I have tried this path as well.

#source = r"C://Pizza//Sammy//Logs//Archive//DataLog_Private//%s//%s//%s//" %(str(folder1),"0" + str(folder2),"0" + str(folder3))
#destination = r"C://Users//alex//Desktop//logPull//DataLog_Private//%s//%s//%s//" %(str(folder1),"0" + str(folder2),"0" + str(folder3))

I am getting this error for both paths, when using copytree

WindowsError: [Error 3] The system cannot find the path specified'C:\\Pizza\\Sammy\\Logs\\Archive\\DataLog_Private\\2018\\08\\03/*.*'

Please help.

Upvotes: 1

Views: 650

Answers (1)

Sam Morgan
Sam Morgan

Reputation: 3308

The following works for me in Python 3.6, note the use of environment variables.

import os
import shutil

folder1 = 2018
folder2 = 8    
folder3 = 3

drive = os.path.join(os.getenv("HOMEDRIVE"), os.sep)
date_path = os.path.join(f"{folder1}", f"{folder2:02}", f"{folder3:02}")

source = os.path.join(
    drive, "Pizza","Sammy", "Logs", "Archive", "DataLog_Private", date_path
)
destination = os.path.join(
    os.getenv("USERPROFILE"), "Desktop", "logPull", "DataLog_Private", date_path
)

shutil.copytree(source, destination)

HOMEDRIVE should point to whatever disk Windows is installed on. List if default environment variables here

The f"{expression}" notation is called an f-string. This was introduced in Python 3.6, here's the PEP. Adding :02 inside the brackets gives the number a leading 0.

Upvotes: 1

Related Questions