Advertisement
AloXado320

python34 download file

Mar 11th, 2025 (edited)
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.73 KB | None | 0 0
  1. import urllib.request
  2. import os
  3. import sys
  4.  
  5. # Check if the file URL argument is provided
  6. if len(sys.argv) < 2:
  7.     print("Usage: python script.py <FileURL>")
  8.     sys.exit(1)
  9.  
  10. # Get the file URL from the command-line argument
  11. url = sys.argv[1]
  12.  
  13. # Determine the script's directory
  14. script_directory = os.path.dirname(os.path.abspath(__file__))
  15.  
  16. # Extract the file name from the URL and set the file path in the script's directory
  17. file_name = os.path.basename(url)
  18. file_path = os.path.join(script_directory, file_name)
  19.  
  20. # Download the file
  21. try:
  22.     urllib.request.urlretrieve(url, file_path)
  23.     print("File downloaded successfully to '" + file_path + "'")
  24. except Exception as e:
  25.     print("An error occurred: " + str(e))
  26.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement