text
stringlengths
0
828
""""""
for cur_file in os.listdir(sourceDir):
if cur_file.lower() == "".ds_store"":
continue
pathWithSource = os.path.join(sourceDir, cur_file)
if include_file or os.path.isdir(pathWithSource):
if include_source:
yield pathWithSource
else:
yield cur_file"
1132,"def copy_dir(sou_dir, dst_dir, del_dst=False, del_subdst=False):
"""""":func:`shutil.copytree()` 也能实现类似功能,
但前者要求目标文件夹必须不存在。
而 copy_dir 没有这个要求,它可以将 sou_dir 中的文件合并到 dst_dir 中。
:param str sou_dir: 待复制的文件夹;
:param str dst_dir: 目标文件夹;
:param bool del_dst: 是否删除目标文件夹。
:param bool del_subdst: 是否删除目标子文件夹。
""""""
if del_dst and os.path.isdir(del_dst):
shutil.rmtree(dst_dir)
os.makedirs(dst_dir, exist_ok=True)
for cur_file in list_dir(sou_dir):
dst_file = os.path.join(dst_dir, cur_file)
cur_file = os.path.join(sou_dir, cur_file)
if os.path.isdir(cur_file):
if del_subdst and os.path.isdir(dst_file):
shutil.rmtree(dst_file)
os.makedirs(dst_file, exist_ok=True)
copy_dir(cur_file, dst_file)
else:
shutil.copyfile(cur_file, dst_file)"
1133,"def get_files(path, ext=[], include=True):
""""""遍历提供的文件夹的所有子文件夹,饭后生成器对象。
:param str path: 待处理的文件夹。
:param list ext: 扩展名列表。
:param bool include: 若值为 True,代表 ext 提供的是包含列表;
否则是排除列表。
:returns: 一个生成器对象。
""""""
has_ext = len(ext)>0
for p, d, fs in os.walk(path):
for f in fs:
if has_ext:
in_ext = False
for name in ext:
if f.endswith(name):
in_ext = True
break
if (include and in_ext) or \
(not include and not in_ext):
yield os.path.join(p,f)
else:
yield os.path.join(p, f)"
1134,"def read_file(file_path, **kws):
""""""读取文本文件的内容。
:param str file_path: 文件路径。
:returns: 文件内容。
:rtype: str
""""""
kw = {""mode"":""r"", ""encoding"":""utf-8""}
if kws:
for k,v in kws.items():
kw[k] = v
with open(file_path, **kw) as afile:
txt = afile.read()
return txt"
1135,"def write_file(file_path, txt, **kws):
""""""将文本内容写入文件。
:param str file_path: 文件路径。
:param str txt: 待写入的文件内容。
""""""
if not os.path.exists(file_path):
upDir = os.path.dirname(file_path)
if not os.path.isdir(upDir):
os.makedirs(upDir)
kw = {""mode"":""w"", ""encoding"":""utf-8""}
if kws:
for k,v in kws.items():
kw[k] = v
with open(file_path, **kw) as afile:
afile.write(txt)"
1136,"def write_by_templ(templ, target, sub_value, safe=False):
""""""根据模版写入文件。
:param str templ: 模版文件所在路径。
:param str target: 要写入的文件所在路径。
:param dict sub_value: 被替换的内容。
""""""
templ_txt = read_file(templ)