Created
May 9, 2014 14:28
-
-
Save EarlGlynn/d583d0da1696ac976a9b to your computer and use it in GitHub Desktop.
This file contains 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
# Python examples using ftplib package. | |
# https://docs.python.org/2/library/ftplib.html | |
# efg, 7 May 2014 | |
# Annoying IDLE setup for up/down arrows on Windows | |
# https://www.clear.rice.edu/engi128/Resources/usingidle.htm | |
# Options | Configure IDLE | Keys | history next and history last | |
import os | |
from ftplib import FTP | |
NewLine = "\n" | |
# Establish local working directory | |
print os.getcwd() # IDLE will set this path | |
os.chdir("C:/2014/Python/FTP/") | |
print os.getcwd(), NewLine | |
# Open FTP site | |
ftp = FTP('ftp.debian.org') | |
ftp.login() | |
print ftp.getwelcome(), NewLine | |
# Establish remote working directory | |
ftp.cwd('debian') | |
print ftp.pwd() | |
# List files in remote directory | |
ftp.retrlines('LIST') | |
# Rename README to README.txt while copying | |
filename = "README" | |
ftp.retrbinary("RETR " + filename, open(filename + '.txt', 'wb').write) | |
# Change to subdirectory at remote site | |
ftp.cwd('doc') | |
print NewLine, ftp.pwd() | |
# Use nlst to get list of filenames | |
print NewLine, "nlst - all" | |
filenames = ftp.nlst() | |
print type(filenames) | |
print filenames | |
print NewLine, "nlst - filtered" | |
filenames = ftp.nlst("bug*.txt") | |
print type(filenames) | |
print filenames | |
# Use dir to get info for selected files | |
# http://effbot.org/librarybook/ftplib.htm | |
print NewLine, "dir - all" | |
filelist = [] | |
ftp.dir(filelist.append) | |
for line in filelist: | |
print "-", line | |
print NewLine, "dir - filtered" | |
filelist = [] | |
ftp.dir("bug*.txt", filelist.append) | |
for line in filelist: | |
print "-", line | |
# Make local copies of set of files | |
print NewLine, "Copying files ..." | |
for filename in filenames: | |
print filename | |
ftp.retrbinary("RETR " + filename, open(filename, 'wb').write) | |
ftp.quit() | |
# Splitting filenames | |
print NewLine, "Splitting Filenames" | |
print filenames[2] | |
print os.path.splitext(filenames[2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment