我想从正在解释的 python 脚本上方的一个文件夹中复制一个文件到 python 脚本所在目录中的一个文件夹。
这是文件夹结构。
class IsProduct {
IsProduct.fromDocument(DocumentSnapshot doc) {
name = doc['name'] as String;
//I tried this but it doesn't work
image = doc['images'][0] == [] ? '' : doc['images'][0] as String;
pid = doc['pid'] as String;
description = doc['description'] as String;
storeId = doc['storeId'] as String;
i = doc['i'] as int;
}
String name;
String image;
String pid;
String storeId;
String description;
int i;
}
我想将文件 -----/A
---------copyme.txt
-----/test
----------myCode.py
---------/B
从文件夹 copyme.txt
复制到 test 文件夹下的文件夹 A
。
这是我试过的代码:
B
但是,我收到此错误:
import os
import shutil
shutil.copyfile('../A/copyme.txt', '/B')
答案 0 :(得分:1)
获取当前目录路径并向其添加src
路径和dest
路径。
例如:
import os
import shutil
BASE_PATH = os.path.dirname(os.path.realpath(__file__))
shutil.copyfile(os.path.join(BASE_PATH, "..", "A", "copyme.txt"),
os.path.join(BASE_PATH, "B", "copyme.txt"))