用到了如下模块:
import os, shutil其中shutil是shell util的缩写。官方文档:https://docs.python.org/3/library/shutil.html
¶ os.path
文档:https://docs.python.org/3/library/os.path.html
判断文件是否存在 os.path.exists
是否为目录 os.path.isdir(path)
¶ cd
os.chdir(path)
它只是libc的chdir的一个wrapper,行为类似于cd -P,在路径中有symbolic
link的时候可能会有问题。
-P use the physical directory structure without following
symbolic links: resolve symbolic links in DIR before
processing instances of `..'
参考:https://bugs.python.org/issue29635
¶ 创建目录
os.makedirs,相当于shell里的mkdir -p,但是如果已经存在则会报错。
os.makedirs(dirname, exist_ok=True)使得即使目录存在也不报错。
¶ 复制文件
shutil.copyfile
¶ 移动
shutil.move