我尝试了不同的堆栈溢出解决方案,但仍然有相同的问题。
localpath = os.path.join(os.sep, 'Users', 'me', 'Desktop', 'ELK', 'PM_XML')
serverpath = r"***/****/***"
def sftp():
ip=ipAddr.get()
while True:
current_time = time.time()
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip,username="root",password="root")
sftp = ssh.open_sftp()
for element in sftp.listdir(serverpath):
if element.endswith(".xml"):
creation_time = sftp.stat(serverpath+element).st_mtime
if (current_time+3400 - creation_time) / (3600) <= 1:
sftp.get(serverpath+element,os.path.join(os.sep,localpath,element))
sftp.close()
ssh.close()
我得到这个错误:
Traceback (most recent call last):
File "<stdin>", line 5, in <module>
File "C:\Users\me\AppData\Local\Programs\Python\Python37-32\lib\site-packages\paramiko\sftp_client.py", line 801, in g
et
with open(localpath, "wb") as fl:
OSError: [Errno 22] Invalid argument: '\\Users\\me\\Desktop\\ELK\\PM_XML\\A2018-10-18T11:03:00+02:00-2018-10-18T11:04:00
+02:00_user-67-0-test-vm2.lk.fr.xml'
A2018-10-18T11:03:00+02:00-2018-10-18T11:04:00+02:00\u用户-67-0-test-vm2.lk.fr.xml'
任何处理这个文件名的建议,因为我想保持服务器上使用的相同名称。
由Martin Prikryl解决
建议将冒号“:”替换为“\”
element.replace(":","_")