Anuj Bhasin
Anuj Bhasin

Reputation: 610

How use os.popen() or os.system() method in python 2.7 for executing a winscp command?

import os

WINSCP = r'C:\Program Files (x86)\WinSCP\WinSCP.com' # the path of winscp in my system
command = "winscp /console /script=SFTP.txt /parameter /opt/outgo/aftp/20161222.zip.pgp C:\Users\Abh\Desktop\\"
stdin, stdout = os.popen2(WINSCP + command)

I am trying to execute this winscp command using popen function of os module to do sftp in client system. However this code is not working as I expect it to. Can you please tell me what is the correct way of doing so, as I am doing it wrong i believe.

This script I am writing for python 2.7 interpreter on windows.

Upvotes: 1

Views: 640

Answers (1)

Anuj Bhasin
Anuj Bhasin

Reputation: 610

import os
import time

year = time.strftime("%Y", time.localtime())
month = time.strftime("%m", time.localtime())
day = int(time.strftime("%d", time.localtime()))-1
WINSCP =r'"C:\\Program Files (x86)\\WinSCP\\WinSCP.com"'
command = WINSCP + " /console /script=SFTP.txt /parameter /opt/outgoing/ttaftp/" + str(year) + str(month) + str(day) +\
       ".zip.pgp C:\\Users\\m\\Desktop\\"
os.system(command)

This script helped me to execute the winscp command and get the desired result.

Upvotes: 1

Related Questions