Last active
September 2, 2017 16:04
-
-
Save mehaase/781dfbb5bf7f37a75a526d46f0b3c619 to your computer and use it in GitHub Desktop.
For writing a file to windows over a command shell.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
if len(sys.argv) < 3: | |
print('Usage: {} <DEST FILE> <INPUT FILE>'.format(sys.argv[0])) | |
sys.exit(1) | |
dest_path = sys.argv[1] | |
in_path = sys.argv[2] | |
def escape(line): | |
escape_chars = ['&', '|', '>', '<', '^'] | |
return ''.join(['^' + c if c in escape_chars else c for c in line]) | |
with open(in_path) as in_file: | |
file_iter = iter(in_file) | |
line = next(file_iter) | |
sys.stdout.write('>{} echo {}'.format(dest_path, escape(line))) | |
for line in file_iter: | |
if line.strip() != '': | |
sys.stdout.write('>>{} echo {}'.format(dest_path, escape(line))) | |
else: | |
sys.stdout.write('>>{} echo.\n'.format(dest_path)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example
Convert an FTP script into a series of echo statements that can be executed in a command shell.
Copy the output and paste it into a shell. It will create
ftp.txt
with the contents of the originaltest
file.It escapes Windows shell characters.