创建目录并将文件写入远程、受密码保护的计算机



我想做的是能够通过网络访问计算机,这样我就可以在那里创建一个新目录,然后在脚本的其余部分中保存多个文件。

我有一个脚本,它创建了一堆我需要保存的文件。问题是该脚本可以在任意数量的计算机上运行,但需要保存到同一台计算机。如果我手动远程连接到计算机,它提示我输入用户名和密码,但我只是试图在那里创建一个目录。下面是我的代码,以及我得到的响应。

if not os.path.exists(self.dirIn):
    tryPath = self.dirIn.split("/")[0:-1]
    tryPath = '/'.join(tryPath)
    if not os.path.exists(tryPath):
        os.mkdir(tryPath)
        os.mkdir(str(self.dirIn))
    else:
        os.mkdir(str(self.dirIn))

WindowsError: [Error 1326]用户名或密码不正确:'//computer/e$/directory/I/am/creating'

我使用的是Windows, Python27

我能够使用subprocess将驱动器映射到我的计算机,做我需要的,然后取消映射驱动器(可选)

subprocess.Popen("net use E: \\computername\E$ %s /USER:%s" % (password, username))
time.sleep(1)  # Short delay to allow E: drive to map before continuing
if not os.path.exists(self.dirIn):
        os.makedirs(self.dirIn)
subprocess.Popen("net use E: /delete")

如果没有休眠,我确实遇到了问题,我的程序找不到目录。

最新更新