- Python Network Programming - Home
- Python Network Introduction
- Python Networking - Environment Setup
- Python Networking - Internet Protocol
- Python Networking - IP Address
- Python Networking - DNS Lookup
- Python Networking - Routing
- Python Networking - HTTP Requests
- Python Networking - HTTP Response
- Python Networking - HTTP Headers
- Python Networking - Custom HTTP Requests
- Python Networking - Request Status Codes
- Python Networking - HTTP Authentication
- Python Networking - HTTP Data Download
- Python Networking - Connection Re-use
- Python Networking - Network Interface
- Python Networking - Sockets Programming
- Python Networking - HTTP Client
- Python Networking - HTTP Server
- Python Networking - Building URLs
- Python Networking - WebForm Submission
- Python Networking - Databases and SQL
- Python Networking - Telnet
- Python Networking - Email Messages
- Python Networking - SMTP
- Python Networking - POP3
- Python Networking - IMAP
- Python Networking - SSH
- Python Networking - FTP
- Python Networking - SFTP
- Python Networking - Web Servers
- Python Networking - Uploading Data
- Python Networking - Proxy Server
- Python Networking - Directory Listing
- Python Networking - Remote Procedure Call
- Python Networking - RPC JSON Server
- Python Networking - Google Maps
- Python Networking - RSS Feed
Python Network Programming Resources
Python Network - Directory Listing
Python can be used to get the list of content from a directory. We can make program to list the content of directory which is in the same machine where python is running. We can also login to the remote system and list the content from the remote directory.
Listing Local Directory
In the below example we use the listdir() method to get the content of the current directory. To also indicate the type of the content like file or directory, we use more functions to evaluate the nature of the content.
main.py
import os
for name in os.listdir('.'):
if os.path.isfile(name):
print('file: ', name)
elif os.path.isdir(name):
print('dir: ', name)
elif os.path.islink(name):
print('link: ', name)
else:
print('unknown', name)
Output
When we run the above program, we get the following output −
file: abcl.htm dir: allbooks link: ulink
Please note the content above is specific to the system where the python program was run. The result will vary depending on the system and its content.
Listing Remote Directory
We can list the content of the remote directory by using ftp to access the remote system. Once the connection is established we can use commands that will list the directory contents in a way similar to the listing of local directories.
from ftplib import FTP
def main():
ftp = FTP('ftp.ibiblio.org')
ftp.login()
ftp.cwd('pub/academic/biology/') # change to some other subject
entries = ftp.nlst()
ftp.quit()
print(len(entries), "entries:")
for entry in sorted(entries):
print(entry)
if __name__ == '__main__':
main()
Output
When we run the above program, we get the following output −
(6, 'entries:') INDEX README acedb dna-mutations ecology+evolution molbio