Skip to content

Instantly share code, notes, and snippets.

@EarlGlynn
Created May 9, 2014 14:30
Show Gist options
  • Save EarlGlynn/1d808077748e55472da0 to your computer and use it in GitHub Desktop.
Save EarlGlynn/1d808077748e55472da0 to your computer and use it in GitHub Desktop.
# Python examples using ftplib package with VMS host.
# Rename VMS files during copy to have version number as part of filename.
# https://docs.python.org/2/library/ftplib.html
# efg, 7 May 2014
import os
from ftplib import FTP
NewLine = "\n"
# Establish local working directory
print os.getcwd() # IDLE will set this path
os.chdir('C:/xxxx/Python/FTP-VAX/')
print os.getcwd(), NewLine
# Open FTP site
ftp = FTP('xx.xxx.xx.x', 'xxxxxxx', 'xxxxxxxx')
# ftp.login() # not needed
print ftp.getwelcome(), NewLine
# Remote working directory
print ftp.pwd()
# Set remote working directory
ftp.cwd('scratch')
print NewLine, "nlst - filtered"
filenames = ftp.nlst('info.txt;*')
print type(filenames)
print filenames
print NewLine, "dir - filtered"
filelist = []
ftp.dir("info.txt;*", filelist.append)
for line in filelist:
print "-", line
# Make local copies of set of files - preserve VMS version in filename
print NewLine, "Copying files (preserve VMS version) ..."
for filename in filenames:
print filename
ftp.retrbinary("RETR " + filename, open(filename, 'wb').write)
# Make local copies of files but move version to filename in front of
# file extension: filename.ext;v -> filename-v.ext
# This enables Windows to interpret file extensions correctly.
print NewLine, "Copying files (move VMS version in new filename) ..."
for filename in filenames:
split1 = os.path.splitext(filename)
split2 = split1[1].split(';')
newfilename = split1[0] + '-' + split2[1] + split2[0]
print filename, '->', newfilename
ftp.retrbinary("RETR " + filename, open(newfilename, 'wb').write)
ftp.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment