[英]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.