-1

I'm trying to upload a simple file from my local host (Windows) to a remote machine (UNIX) using Python 3.3

Here's the code:

import os
import Crypto
import paramiko
import pysftp

localpath = "C:\\py.txt"
remotepath = "/tmp/py.txt"

s = pysftp.Connection(host='10.1.1.1', username='user', password='pass')

s.put(localpath, remotepath)

The error which returns is:

Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    s.put(localpath, remotepath)
  File "C:\Python33\lib\site-packages\pysftp.py", line 349, in put
    confirm=confirm)
  File "C:\Python33\lib\site-packages\paramiko-1.14.0-       py3.3.egg\paramiko\sftp_client.py", line 585, in put
    file_size = os.stat(localpath).st_size
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C://py.txt'

I've tried different prefix for localpath, like 'C:\py.txt' but I get the same results.

Thanks in advance

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Chen A.
  • 10,140
  • 3
  • 42
  • 61

2 Answers2

0

Python uses backslash to quote characters, like \n = newline and \t = tab; thus \\\\ = one slash.

Use an r prefix to make a raw string literal:

localpath = r"C:\\py.txt"
johntellsall
  • 14,394
  • 4
  • 46
  • 40
0

I've found the problem. The problem was with the file location, as it's name was 'py.txt' but my windows didn't show file extensions, so the actual file name was py.txt.txt

Chen A.
  • 10,140
  • 3
  • 42
  • 61
  • 2
    That is why I asked you before that if you have a file named `py.txt`. :) In your case the file name is `py.txt.txt` Don't forget to accept your answer too . – ρss Jun 04 '14 at 12:16