Upload files using SFTP in Python, but create directories if path doesn't exist
I want to upload a file on a remote server with Python. I'd like to check beforehand if the remote path is really existing, and if it isn't, to create it. In pseudocode: if(remote_path not exist): create_path(remote_path) upload_file(local_file, remote_path) I was thinking about executing a command in Paramiko to create the path (e.g. mkdir -p remote_path ). I came up with this: # I didn't test this code import paramiko, sys ssh = paramiko.SSHClient() ssh.connect(myhost, 22, myusername, mypassword) ssh.exec_command('mkdir -p ' + remote_path) ssh.close transport = paramiko.Transport((myhost, 22