【发布时间】:2016-02-11 02:10:56
【问题描述】:
我尝试使用pathlib 复制文件
import pathlib
import shutil
my_file=pathlib.Path('/etc/hosts')
to_file=pathlib.Path('/tmp/foo')
shutil.copy(my_file, to_file)
我得到了这个例外:
/home/foo_egs_d/bin/python /home/foo_egs_d/src/test-pathlib-copy.py
Traceback (most recent call last):
File "/home/foo_egs_d/src/test-pathlib-copy.py", line 6, in <module>
shutil.copy(my_file, to_file)
File "/usr/lib/python2.7/shutil.py", line 117, in copy
if os.path.isdir(dst):
File "/home/foo_egs_d/lib/python2.7/genericpath.py", line 41, in isdir
st = os.stat(s)
TypeError: coercing to Unicode: need string or buffer, PosixPath found
Process finished with exit code
...如何在 Python 2.7 中使用 pathlib 复制文件?
【问题讨论】:
-
这在 Python 3.6 上不会出错
-
@Anthon 我们使用 Python 2.7。
标签: python file copy python-2.x pathlib