简体   繁体   中英

How to run commands in a external file through command prompt using python?

My Requirement is -

I have set of commands written in a "commands.ftp" file, to upload a "request.txt" file to FTP server. I want to call the command prompt from my python file and run the commands in the "commands.ftp" file one by one . Once all the command executed successfully I need the output in my python file. Suppose if the file is uploaded successfully or if any error occurred.

I saw many answers where it was recommended to use subprocess. but I am not getting exact answer to use this.

command.ftp

ftp
open hostname.com
usename
password    
quote site recfm=fb lrecl=750 blocksize=27750 cyl pri=120 sec=60
put "File_Name.txt"
QUIT

The above code is to upload a file to Mainframe CMS. I am able to upload the file successfully. But After uploading the file the response which we will receive how to capture that . I am not able to capture the output response.

Below is the code I am writing to to execute the command.ftp file.

from subprocess import Popen, PIPE

commands = open('local.ftp', 'r').read()

process = Popen("cmd.exe", shell=False, universal_newlines=True,
            stdin=PIPE, stdout=PIPE, stderr=PIPE)
out = process.communicate(commands)
print(out)

Here the out variable is not giving the output

Python has an FTP module built-in ( import ftplib ). That would be WAY smarter than trying to pass commands into the command-line "ftp" command. Windows, for example, doesn't include an "ftp" command in the box at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM