1

I am trying to copy specific files from one folder to another but I get an error I don't understand why :

import os
import shutil

def setPath_getData():        
        # Set up folders for data
    newpath = r'userdata' 
    if not os.path.exists(newpath):
        os.makedirs(newpath)
        os.makedirs('userdata/sleep')
        os.makedirs(r'userdata/distance')
        os.makedirs(r'userdata/steps')
        os.makedirs(r'userdata/lightly')
        os.makedirs(r'userdata/mod')
        os.makedirs(r'userdata/sedentary')
        os.makedirs(r'userdata/very')
        os.makedirs(r'userdata/heart-rate-zone')
        os.makedirs(r'userdata/heart-rate')
        
        
            # Get data from fitbit
        filenames = os.listdir("user-site-export")
        unique_filenames = set()
        for f in filenames:
            unique_filenames.add(f.split("-")[0])
        
        source = os.listdir('user-site-export/')
        dest = '/userdata/sleep/'
        
        for file in source:
            if file.startswith('sleep'):
                shutil.copy(file, dest)
            
            #ls userdata/
    print("Data loaded successfully")

setPath_getData()

the error it gives is :

FileNotFoundError: [Errno 2] No such file or directory: 'sleep-2020-01-09.json'

So it looks like it is fetching the correct files but it does not copy them to dest. Any ideas why ?

1 Answer 1

1

You must specify the source path before the file variable in the copy command: shutil.copy(os.path.join("user-site-export", file), dest)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.