简体   繁体   English

将具有特定名称的文件从一个文件夹复制到 python 中的另一个文件夹

[英]Copy file with specific name from one folder to another in python

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.所以看起来它正在获取正确的文件,但它没有将它们复制到 dest。 Any ideas why?任何想法为什么?

You must specify the source path before the file variable in the copy command: shutil.copy(os.path.join("user-site-export", file), dest)您必须在复制命令中的文件变量之前指定源路径: shutil.copy(os.path.join("user-site-export", file), dest)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有办法使用 python 中该文件名的子文本将文件从一个文件夹复制到另一个文件夹? - Is there a way to copy files from one folder to another using a subtext of that file name in python? Python 3,将特定列从一个csv文件复制到另一个csv - Python 3, Copy specific column from one csv file to another csv 如何将一种特定类型的所有文件从一个文件夹复制到Python中的另一个文件夹 - How do I copy all files of one specific type from a folder to another folder in Python 在python中使用哪种文件shutil复制方法将某些文件从一个文件夹复制到另一个文件夹? - Which file shutil copy method to use in python to copy certain files from one folder to another? 如何使用 Python 将 zipfile 从一个文件夹复制到另一个文件夹 - How to copy zipfile from one folder to another folder using Python Python 将文件夹中的粘贴文件剪切/复制到另一个文件夹 - Python Cut/Copy paste file from folder to another folder python - 将列从一个文件复制到另一个文件 - python - copy columns from one file to another 在 Python 中将文件从一个位置复制到另一个位置 - Copy a file from one location to another in Python 将列表从一个文件复制到另一个文件(python) - Copy a list from one file to another (python) Python [Errno 22] 将文件从一个文件夹复制到另一个文件夹时参数无效 - Python [Errno 22] Invalid argument when copy a file from one folder to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM