我想用Python在本地网络中的两台Windows 10计算机之间以编程方式传输文件。
我试过使用shutil;但是,我不知道如何指定目标计算机中文件夹的路径。
下面是我尝试的代码,但是它给了我一个错误声明&;Invalid argument &;。我认为目的地路径指定不正确。
你能帮帮我吗?
import os
import shutil
import ntpath
ip_address="192.168.1.2"
source_file = "D:\Test\testfile.txt"
destination_file = ip_address + D:\Test\testfile.txt"
shutil.copyfile(source_file, destination_file)
您获得OSError: [Errno 22] Invalid argument
的原因可能是所讨论的文件不再(或尚未)与收件人共享。
在Windows文档中,您可以通过以下方式访问特定文件:
- 右键单击(或长按)一个文件,然后选择:
显示更多选项>>>>特定的人。- 在网络中选择一个用户共享文件,或者选择"Everyone",允许所有网络用户访问该文件。
只有这样,您才能使用shutil.copyfile
并重新运行脚本:
import os
import shutil
import ntpath
ip_address ="192.168.1.2"
source_file = r"D:Testtestfile.txt"
destination_file = ip_address + r"Testtestfile.txt"
shutil.copyfile(source_file, destination_file)
注意:您必须确保收件人的计算机中存在Test
文件夹并且是可写的。