如何将文件从Windows复制到Windows/Linux uisng Python脚本
问题描述:
我需要将文件从Windows复制到Linux和具有相同逻辑的Windows机器。可能吗?如何将文件从Windows复制到Windows/Linux uisng Python脚本
例:
from shutil import copyfile
copyfile(src, dst)
# src im giving as "C:\Temp\x.txt"
dst = "\\xxx.xxx.xxx.xxx\Test"
显示错误:
No such File or Directory : "\xxx.xxx.xxx.xxx\Test"
答
Windows和Linux的目录结构是不同的。在Windows上复制某些内容到C:\Program Files
可能很有意义,但在Linux中,您不会将应用程序存储在/Program Files
中。但是,您可以执行的操作是存储相对于以下某个位置的文件。
主目录
pathlib.Path.home() or os.path.expanduser("~")
当前工作目录
pathlib.Path.cwd() or os.getcwd()
当前的Python文件
__FILE__
临时目录
tempfile.gettempdir()
一旦你有你会从开始的文件夹,然后可以得到跨平台的子文件夹,如下所示。
在Python 3.4+,您会使用pathlib模块
pathlib.Path.home()/"path"/"to"/"somewhere"
而在旧版本的Python你可以使用os.path中模块
os.path.join(os.path.expanduser("~"), "path", "to", "somewhere")